コード例 #1
0
ファイル: state.go プロジェクト: cayleydb/dgraph
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)
}
コード例 #2
0
ファイル: state.go プロジェクト: cayleydb/dgraph
func lexArgumentVal(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case isSpace(r):
			l.Ignore()
		}
	}
}
コード例 #3
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexPredicate(l *lex.Lexer) lex.StateFn {
	r := l.Next()
	// The predicate can only be an IRI according to the spec.
	if r != '<' {
		return l.Errorf("Invalid character in lexPredicate: %v", r)
	}

	l.Depth++
	return lexUntilClosing(l, itemPredicate, lexText)
}
コード例 #4
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexObject(l *lex.Lexer) lex.StateFn {
	r := l.Next()
	// The object can be an IRI, blank node or a literal.
	if r == '<' {
		l.Depth++
		return lexUntilClosing(l, itemObject, lexText)
	}

	if r == '_' {
		l.Depth++
		r = l.Next()
		// TODO - Remove this, doesn't conform to the spec.
		if r == 'u' {
			return lexUidNode(l, itemObject, lexText)
		}

		if r == ':' {
			return lexBlankNode(l, itemObject, lexText)
		}
	}
	if r == '"' {
		l.Ignore()
		return lexLiteral(l)
	}

	return l.Errorf("Invalid char: %v at lexObject", r)
}
コード例 #5
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexTextMutation lexes and absorbs the text inside a mutation operation block.
func lexTextMutation(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if r == lex.EOF {
			return l.Errorf("Unclosed mutation text")
		}
		if r == quote {
			return lexMutationValue
		}
		if r == leftCurl {
			l.Depth++
		}
		if r == rightCurl {
			if l.Depth > 2 {
				l.Depth--
				continue
			}
		}
		if r != rightCurl {
			// Absorb everything until we find '}'.
			continue
		}
		l.Backup()
		l.Emit(itemMutationContent)
		break
	}
	return lexInsideMutation
}
コード例 #6
0
ファイル: state.go プロジェクト: cayleydb/dgraph
func lexUntilClosing(l *lex.Lexer, styp lex.ItemType,
	sfn lex.StateFn) lex.StateFn {

	l.AcceptUntil(isClosingBracket)
	r := l.Next()
	if r == lex.EOF {
		return l.Errorf("Unexpected end of subject")
	}
	if r == '>' {
		l.Emit(styp)
		return sfn
	}
	return l.Errorf("Invalid character %v found for itemType: %v", r, styp)
}
コード例 #7
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexAlias(l *lex.Lexer) lex.StateFn {
	l.AcceptRun(isSpace)
	l.Ignore() // Any spaces encountered.
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue
		}
		l.Backup()
		l.Emit(itemAlias)
		break
	}
	return lexInside
}
コード例 #8
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexScalarBlock(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case r == ')':
			l.Emit(itemRightRound)
			return lexText
		case isNameBegin(r):
			l.Backup()
			return lexScalarPair1
		case isSpace(r) || isEndOfLine(r):
			l.Ignore()
		default:
			return l.Errorf("Invalid schema. Unexpected %s", l.Input[l.Start:l.Pos])
		}
	}
}
コード例 #9
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexObjectBlock(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case r == leftCurl:
			l.Emit(itemLeftCurl)
		case isSpace(r) || isEndOfLine(r):
			l.Ignore()
		case r == rightCurl:
			l.Emit(itemRightCurl)
			return lexText
		case isNameBegin(r):
			return lexObjectPair
		default:
			return l.Errorf("Invalid schema. Unexpected %s", l.Input[l.Start:l.Pos])
		}
	}
}
コード例 #10
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// This function is used to absorb the object value.
func lexMutationValue(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if r == '\\' {
			// So that we don't count \" as end of value.
			if l.Next() == quote {
				continue
			}

		}
		// This is an end of value so lets return.
		if r == quote {
			break
		}
		// We absorb everything else.
		continue
	}
	return lexTextMutation
}
コード例 #11
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// Assumes that the current rune is '_'.
func lexBlankNode(l *lex.Lexer, styp lex.ItemType,
	sfn lex.StateFn) lex.StateFn {

	// RDF Blank Node.
	// TODO: At some point do checkings based on the guidelines. For now,
	// just accept everything until space.
	l.AcceptUntil(isSpace)
	r := l.Peek()
	if r == lex.EOF {
		return l.Errorf("Unexpected end of subject")
	}

	if isSpace(r) {
		l.Emit(styp)
		return sfn
	}

	return l.Errorf("Invalid character %v found for itemType: %v", r, styp)
}
コード例 #12
0
ファイル: state.go プロジェクト: ashwin95r/dgraph
func lexOperationType(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue // absorb
		}
		l.Backup()
		word := l.Input[l.Start:l.Pos]
		if word == "mutation" {
			l.Emit(itemOpType)
			l.Mode = mutationMode

		} else if word == "query" {
			l.Emit(itemOpType)
			l.Mode = queryMode
		}
		break
	}
	return lexText
}
コード例 #13
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexLabel(l *lex.Lexer) lex.StateFn {
	r := l.Next()
	// Graph label can either be an IRI or a blank node according to spec.
	if r == '<' {
		l.Depth++
		return lexUntilClosing(l, itemLabel, lexText)
	}

	if r == '_' {
		l.Depth++
		r = l.Next()
		if r != ':' {
			return l.Errorf("Invalid char: %c at lexLabel", r)
		}

		l.Backup()
		return lexBlankNode(l, itemLabel, lexText)
	}
	return l.Errorf("Invalid char: %v at lexLabel", r)
}
コード例 #14
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexStart(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue // absorb
		}
		l.Backup()
		// l.Pos would be index of the end of operation type + 1.
		word := l.Input[l.Start:l.Pos]
		if word == "scalar" {
			l.Emit(itemScalar)
			return lexScalar
		} else if word == "type" {
			l.Emit(itemType)
			return lexObject
		} else {
			return l.Errorf("Invalid schema")
		}
	}

}
コード例 #15
0
ファイル: state.go プロジェクト: cayleydb/dgraph
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)
}
コード例 #16
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexComment lexes a comment text.
func lexComment(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isEndOfLine(r) {
			l.Emit(itemComment)
			return lexInside
		}
		if r == lex.EOF {
			break
		}
	}
	if l.Pos > l.Start {
		l.Emit(itemComment)
	}
	l.Emit(lex.ItemEOF)
	return nil // Stop the run loop.
}
コード例 #17
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexFragmentSpread(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue
		}
		l.Backup()
		l.Emit(itemFragmentSpread)
		break
	}
	return lexInside
}
コード例 #18
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexArgName lexes and emits the name part of an argument.
func lexArgName(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue
		}
		l.Backup()
		l.Emit(itemArgName)
		break
	}
	return lexArgInside
}
コード例 #19
0
ファイル: state.go プロジェクト: cayleydb/dgraph
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)
}
コード例 #20
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexFilterFuncName expects input to look like equal("...", "...").
func lexFilterFuncName(l *lex.Lexer) lex.StateFn {
	for {
		// The caller already checked isNameBegin, and absorbed one rune.
		r := l.Next()
		if isNameSuffix(r) {
			continue
		}
		l.Backup()
		l.Emit(itemFilterFunc)
		break
	}
	return lexFilterFuncInside
}
コード例 #21
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexNameMutation lexes the itemMutationOp, which could be set or delete.
func lexNameMutation(l *lex.Lexer) lex.StateFn {
	for {
		// The caller already checked isNameBegin, and absorbed one rune.
		r := l.Next()
		if isNameBegin(r) {
			continue
		}
		l.Backup()
		l.Emit(itemMutationOp)
		break
	}
	return lexInsideMutation
}
コード例 #22
0
ファイル: state.go プロジェクト: cayleydb/dgraph
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)
}
コード例 #23
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexSubject(l *lex.Lexer) lex.StateFn {
	r := l.Next()
	// The subject is an IRI, so we lex till we encounter '>'.
	if r == '<' {
		l.Depth++
		return lexUntilClosing(l, itemSubject, lexText)
	}

	// The subject represents a blank node.
	if r == '_' {
		l.Depth++
		r = l.Next()
		// TODO - Remove this, doesn't conform to the spec.
		if r == 'u' {
			return lexUidNode(l, itemSubject, lexText)
		}

		if r == ':' {
			return lexBlankNode(l, itemSubject, lexText)
		}
	}
	// else go to error
	return l.Errorf("Invalid character during lexSubject: %v", r)
}
コード例 #24
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexText lexes the input string and calls other lex functions.
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++        // one level down.
			l.Emit(itemLeftCurl)
			if l.Mode == mutationMode {
				return lexInsideMutation
			}
			// Both queryMode and fragmentMode are handled by lexInside.
			return lexInside
		case r == rightCurl:
			return l.Errorf("Too many right characters")
		case r == lex.EOF:
			break Loop
		case r == leftRound:
			l.Backup()
			l.Emit(itemText)
			l.Next()
			l.Emit(itemLeftRound)
			return lexVarInside
		case isNameBegin(r):
			l.Backup()
			l.Emit(itemText)
			return lexOperationType
		}
	}
	if l.Pos > l.Start {
		l.Emit(itemText)
	}
	l.Emit(lex.ItemEOF)
	return nil
}
コード例 #25
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexArgVal lexes and emits the value part of an argument.
func lexArgVal(l *lex.Lexer) lex.StateFn {
	l.AcceptRun(isSpace)
	l.Ignore() // Any spaces encountered.
	for {
		r := l.Next()
		if isSpace(r) || isEndOfLine(r) || r == rightRound || r == comma {
			l.Backup()
			l.Emit(itemArgVal)
			return lexArgInside
		}
		if r == lex.EOF {
			return l.Errorf("Reached lex.EOF while reading var value: %v",
				l.Input[l.Start:l.Pos])
		}
	}
}
コード例 #26
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexArgInside is used to lex the arguments inside ().
func lexArgInside(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case r == lex.EOF:
			return l.Errorf("unclosed argument")
		case isSpace(r) || isEndOfLine(r) || r == comma:
			l.Ignore()
		case isNameBegin(r):
			return lexArgName
		case r == ':':
			l.Ignore()
			return lexArgVal
		case r == rightRound:
			l.Emit(itemRightRound)
			return lexInside
		default:
			return l.Errorf("argument list invalid")
		}
	}
}
コード例 #27
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
func lexVarInside(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case r == lex.EOF:
			return l.Errorf("unclosed argument")
		case isSpace(r) || isEndOfLine(r):
			l.Ignore()
		case isDollar(r):
			return lexVarName
		case r == ':':
			l.Ignore()
			return lexVarType
		case r == equal:
			l.Emit(itemEqual)
			l.Ignore()
			return lexVarDefault
		case r == rightRound:
			l.Emit(itemRightRound)
			return lexText
		case r == comma:
			l.Emit(itemComma)
			l.Ignore()
		default:
			return l.Errorf("variable list invalid")
		}
	}
}
コード例 #28
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexOperationType lexes a query or mutation operation type.
func lexOperationType(l *lex.Lexer) lex.StateFn {
	for {
		r := l.Next()
		if isNameSuffix(r) {
			continue // absorb
		}
		l.Backup()
		// l.Pos would be index of the end of operation type + 1.
		word := l.Input[l.Start:l.Pos]
		if word == "mutation" {
			l.Emit(itemOpType)
			l.Mode = mutationMode
		} else if word == "fragment" {
			l.Emit(itemOpType)
			l.Mode = fragmentMode
		} else if word == "query" {
			l.Emit(itemOpType)
			l.Mode = queryMode
		} else {
			if l.Mode == 0 {
				l.Errorf("Invalid operation type")
			}
		}
		break
	}
	return lexText
}
コード例 #29
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexDirective is called right after we see a @.
func lexDirective(l *lex.Lexer) lex.StateFn {
	r := l.Next()
	if !isNameBegin(r) {
		return l.Errorf("Unrecognized character in lexDirective: %#U", r)
	}

	l.Backup()
	// This gives our buffer an initial capacity. Its length is zero though. The
	// buffer can grow beyond this initial capacity.
	buf := bytes.NewBuffer(make([]byte, 0, 15))
	for {
		// The caller already checked isNameBegin, and absorbed one rune.
		r = l.Next()
		buf.WriteRune(r)
		if isNameSuffix(r) {
			continue
		}
		l.Backup()
		l.Emit(itemDirectiveName)

		directive := buf.Bytes()[:buf.Len()-1]
		// The lexer may behave differently for different directives. Hence, we need
		// to check the directive here and go into the right state.
		if string(directive) == "filter" {
			return lexFilterInside
		}
		return l.Errorf("Unhandled directive %s", directive)
	}
	return lexInside
}
コード例 #30
0
ファイル: state.go プロジェクト: dgraph-io/dgraph
// lexInsideMutation lexes the text inside a mutation block.
func lexInsideMutation(l *lex.Lexer) lex.StateFn {
	for {
		switch r := l.Next(); {
		case r == rightCurl:
			l.Depth--
			l.Emit(itemRightCurl)
			if l.Depth == 0 {
				return lexText
			}
		case r == leftCurl:
			l.Depth++
			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)
		}
	}
}