func lexInside(l *lex.Lexer) lex.StateFn { for { switch r := l.Next(); { case r == rightCurl: l.Depth -= 1 l.Emit(itemRightCurl) if l.Depth == 0 { return lexText } case r == leftCurl: l.Depth += 1 l.Emit(itemLeftCurl) case r == lex.EOF: return l.Errorf("unclosed action") case isSpace(r) || isEndOfLine(r) || r == ',': l.Ignore() case isNameBegin(r): return lexName case r == '#': l.Backup() return lexComment case r == '(': l.Emit(itemLeftRound) return lexArgInside default: return l.Errorf("Unrecognized character in lexInside: %#U", r) } } }
func lexInsideMutation(l *lex.Lexer) lex.StateFn { for { switch r := l.Next(); { case r == rightCurl: l.Depth -= 1 l.Emit(itemRightCurl) if l.Depth == 0 { return lexText } case r == leftCurl: l.Depth += 1 l.Emit(itemLeftCurl) if l.Depth >= 2 { return lexTextMutation } case r == lex.EOF: return l.Errorf("Unclosed mutation action") case isSpace(r) || isEndOfLine(r): l.Ignore() case isNameBegin(r): return lexNameMutation default: return l.Errorf("Unrecognized character in lexInsideMutation: %#U", r) } } }
func lexLabel(l *lex.Lexer) lex.StateFn { r := l.Next() if r == '<' { l.Depth += 1 return lexUntilClosing(l, itemLabel, lexText) } if r == '_' { l.Depth += 1 return lexBlankNode(l, itemLabel, lexText) } return l.Errorf("Invalid char: %v at lexLabel", r) }
func lexSubject(l *lex.Lexer) lex.StateFn { r := l.Next() if r == '<' { l.Depth += 1 return lexUntilClosing(l, itemSubject, lexText) } if r == '_' { l.Depth += 1 return lexBlankNode(l, itemSubject, lexText) } return l.Errorf("Invalid character during lexSubject: %v", r) }
func lexText(l *lex.Lexer) lex.StateFn { Loop: for { switch r := l.Next(); { case r == leftCurl: l.Backup() l.Emit(itemText) // emit whatever we have so far. l.Next() // advance one to get back to where we saw leftCurl. l.Depth += 1 // one level down. l.Emit(itemLeftCurl) return lexInside // we're in. case r == rightCurl: return l.Errorf("Too many right characters") case r == lex.EOF: break Loop case isNameBegin(r): l.Backup() l.Emit(itemText) return lexOperationType } } if l.Pos > l.Start { l.Emit(itemText) } l.Emit(lex.ItemEOF) return nil }
// Assumes '"' has already been encountered. func lexLiteral(l *lex.Lexer) lex.StateFn { for { r := l.Next() if r == '\u005c' { // backslash r = l.Next() continue // This would skip over the escaped rune. } if r == lex.EOF || isEndLiteral(r) { break } } l.Backup() l.Emit(itemLiteral) l.Next() // Move to end literal. l.Ignore() // Ignore end literal. l.Depth += 1 r := l.Peek() if r == '@' { return lexLanguage(l) } else if r == '^' { return lexObjectType(l) } else { return lexText } }
func lexObject(l *lex.Lexer) lex.StateFn { r := l.Next() if r == '<' { l.Depth += 1 return lexUntilClosing(l, itemObject, lexText) } if r == '_' { l.Depth += 1 return lexBlankNode(l, itemObject, lexText) } if r == '"' { l.Ignore() return lexLiteral(l) } return l.Errorf("Invalid char: %v at lexObject", r) }
func lexPredicate(l *lex.Lexer) lex.StateFn { r := l.Next() if r != '<' { return l.Errorf("Invalid character in lexPredicate: %v", r) } l.Depth += 1 return lexUntilClosing(l, itemPredicate, lexText) }