Ejemplo n.º 1
0
func (parser *Parser) expectNumeric(fmtStr string, args ...interface{}) (number *ast.Numeric) {
	msg := fmt.Sprintf(fmtStr, args...)

	for {
		token := parser.next()

		parser.D("expect numeric :%s", token)

		if token.Type == lexer.TokenINT {
			number = ast.NewNumeric(float64(token.Value.(int64)))
		} else if token.Type == lexer.TokenFLOAT {
			number = ast.NewNumeric(token.Value.(float64))
		}

		if number != nil {
			return number
		}

		parser.errorf(token.Start, msg)
	}

}
Ejemplo n.º 2
0
func (parser *Parser) expectExpr(fmtStr string, args ...interface{}) ast.Expr {

	msg := fmt.Sprintf(fmtStr, args...)

	for {
		token := parser.peek()

		var expr ast.Expr

		switch token.Type {

		case lexer.TokenINT:
			parser.next()

			expr = ast.NewNumeric(float64(token.Value.(int64)))

			_setNodePos(expr, token.Start, token.End)

		case lexer.TokenFLOAT:

			parser.next()

			expr = ast.NewNumeric(token.Value.(float64))

			_setNodePos(expr, token.Start, token.End)

		case lexer.TokenSTRING:
			parser.next()

			expr = ast.NewString(token.Value.(string))

			_setNodePos(expr, token.Start, token.End)

		case lexer.TokenTrue:
			parser.next()

			expr = ast.NewBoolean(true)

			_setNodePos(expr, token.Start, token.End)

		case lexer.TokenFalse:
			parser.next()

			expr = ast.NewBoolean(false)

			_setNodePos(expr, token.Start, token.End)

		case lexer.TokenID:
			name, start, end := parser.expectFullName("expect constant reference or table instance")

			token = parser.peek()

			if token.Type == lexer.TokenType('(') {
				initargs := parser.expectArgsTable("expect table instance init args table")
				newObj := ast.NewNewObj(name, initargs)

				_setNodePos(newObj, start, end)

				return newObj
			}

			expr = ast.NewConstantRef(name)

			_setNodePos(expr, start, end)
		}

		if expr != nil {

			token := parser.peek()

			switch token.Type {
			case lexer.OpBitOr, lexer.OpBitAnd:

				parser.next()

				rhs := parser.expectExpr("expect binary op(%s) rhs", token.Type)

				binaryOp := ast.NewBinaryOp(token.Type, expr, rhs)

				start, _ := Pos(expr)

				_, end := Pos(binaryOp)

				_setNodePos(binaryOp, start, end)

				expr = binaryOp
			}

			return expr
		}

		parser.errorf(token.Start, msg)

		parser.next()
	}
}