Skip to content

Commit

Permalink
Merge pull request #570 from goby-lang/namespace-parser-constants
Browse files Browse the repository at this point in the history
Namespace parser constants
  • Loading branch information
st0012 authored Jan 11, 2018
2 parents a03f7c7 + 63f99c6 commit c00ad0c
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 246 deletions.
33 changes: 33 additions & 0 deletions compiler/parser/arguments/arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package arguments

import "github.com/goby-lang/goby/compiler/token"

// Enums for different kinds of arguments
const (
NormalArg = iota
OptionedArg
SplatArg
RequiredKeywordArg
OptionalKeywordArg
)

// Types is a table maps argument types enum to the their real name
var Types = map[int]string{
NormalArg: "Normal argument",
OptionedArg: "Optioned argument",
RequiredKeywordArg: "Keyword argument",
OptionalKeywordArg: "Optioned keyword argument",
SplatArg: "Splat argument",
}

// Tokens marks token types that can be used as method call arguments
var Tokens = map[token.Type]bool{
token.Int: true,
token.String: true,
token.True: true,
token.False: true,
token.Null: true,
token.InstanceVariable: true,
token.Ident: true,
token.Constant: true,
}
16 changes: 9 additions & 7 deletions compiler/parser/data_type_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package parser
import (
"fmt"
"github.com/goby-lang/goby/compiler/ast"
"github.com/goby-lang/goby/compiler/parser/errors"
"github.com/goby-lang/goby/compiler/parser/precedence"
"github.com/goby-lang/goby/compiler/token"
"strconv"
)
Expand All @@ -12,7 +14,7 @@ func (p *Parser) parseIntegerLiteral() ast.Expression {

value, err := strconv.ParseInt(lit.TokenLiteral(), 0, 64)
if err != nil {
p.error = newTypeParsingError(lit.TokenLiteral(), "integer", p.curToken.Line)
p.error = errors.NewTypeParsingError(lit.TokenLiteral(), "integer", p.curToken.Line)
return nil
}

Expand All @@ -33,7 +35,7 @@ func (p *Parser) parseFloatLiteral(integerPart ast.Expression) ast.Expression {
lit := &ast.FloatLiteral{BaseNode: &ast.BaseNode{Token: floatTok}}
value, err := strconv.ParseFloat(lit.TokenLiteral(), 64)
if err != nil {
p.error = newTypeParsingError(lit.TokenLiteral(), "float", p.curToken.Line)
p.error = errors.NewTypeParsingError(lit.TokenLiteral(), "float", p.curToken.Line)
return nil
}
lit.Value = float64(value)
Expand All @@ -52,7 +54,7 @@ func (p *Parser) parseBooleanLiteral() ast.Expression {

value, err := strconv.ParseBool(lit.TokenLiteral())
if err != nil {
p.error = newTypeParsingError(lit.TokenLiteral(), "boolean", p.curToken.Line)
p.error = errors.NewTypeParsingError(lit.TokenLiteral(), "boolean", p.curToken.Line)
return nil
}

Expand Down Expand Up @@ -104,7 +106,7 @@ func (p *Parser) parseHashPair(pairs map[string]ast.Expression) {
case token.Constant, token.Ident:
key = p.parseIdentifier().(ast.Variable).ReturnValue()
default:
p.error = newTypeParsingError(p.curToken.Literal, "hash key", p.curToken.Line)
p.error = errors.NewTypeParsingError(p.curToken.Literal, "hash key", p.curToken.Line)
return
}

Expand All @@ -113,7 +115,7 @@ func (p *Parser) parseHashPair(pairs map[string]ast.Expression) {
}

p.nextToken()
value = p.parseExpression(NORMAL)
value = p.parseExpression(precedence.Normal)
pairs[key] = value
}

Expand All @@ -132,12 +134,12 @@ func (p *Parser) parseArrayElements() []ast.Expression {
}

p.nextToken() // start of first expression
elems = append(elems, p.parseExpression(NORMAL))
elems = append(elems, p.parseExpression(precedence.Normal))

for p.peekTokenIs(token.Comma) {
p.nextToken() // ","
p.nextToken() // start of next expression
elems = append(elems, p.parseExpression(NORMAL))
elems = append(elems, p.parseExpression(precedence.Normal))
}

if !p.expectPeek(token.RBracket) {
Expand Down
61 changes: 61 additions & 0 deletions compiler/parser/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package errors

import (
"fmt"
"github.com/goby-lang/goby/compiler/parser/arguments"
)

// Enums for different kinds of syntax errors
const (
_ = iota
// EndOfFileError represents normal EOF error
EndOfFileError
// UnexpectedTokenError means that token is not what we expected
UnexpectedTokenError
// UnexpectedEndError means we get unexpected "end" keyword (this is mainly created for REPL)
UnexpectedEndError
// MethodDefinitionError means there's an error on method definition's method name
MethodDefinitionError
// InvalidAssignmentError means user assigns value to wrong type of expressions
InvalidAssignmentError
// SyntaxError means there's a grammatical in the source code
SyntaxError
// ArgumentError means there's a method parameter's definition error
ArgumentError
)

// Error represents parser's parsing error
type Error struct {
// Message contains the readable message of error
Message string
ErrType int
}

// IsEOF checks if error is end of file error
func (e *Error) IsEOF() bool {
return e.ErrType == EndOfFileError
}

// IsUnexpectedEnd checks if error is unexpected "end" keyword error
func (e *Error) IsUnexpectedEnd() bool {
return e.ErrType == UnexpectedEndError
}

// InitError is a helper function for easily initializing error object
func InitError(msg string, errType int) *Error {
return &Error{Message: msg, ErrType: errType}
}

// NewArgumentError is a helper function the helps initializing argument errors
func NewArgumentError(formerArgType, laterArgType int, argLiteral string, line int) *Error {
formerArg := arguments.Types[formerArgType]
laterArg := arguments.Types[laterArgType]
msg := fmt.Sprintf("%s \"%s\" should be defined before %s. Line: %d", formerArg, argLiteral, laterArg, line)
return InitError(msg, ArgumentError)
}

// NewTypeParsingError is a helper function the helps initializing type parsing errors
func NewTypeParsingError(tokenLiteral, targetType string, line int) *Error {
msg := fmt.Sprintf("could not parse %q as %s. Line: %d", tokenLiteral, targetType, line)
return InitError(msg, SyntaxError)
}
21 changes: 21 additions & 0 deletions compiler/parser/events/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package events

import (
"github.com/goby-lang/goby/compiler/parser/states"
)

// These are state machine's events
const (
BackToNormal = "backToNormal"
ParseFuncCall = "parseFuncCall"
ParseMethodParam = "parseMethodParam"
ParseAssignment = "parseAssignment"
)

// EventTable is the mapping of state and its corresponding event
var EventTable = map[string]string{
states.Normal: BackToNormal,
states.ParsingFuncCall: ParseFuncCall,
states.ParsingMethodParam: ParseMethodParam,
states.ParsingAssignment: ParseAssignment,
}
Loading

0 comments on commit c00ad0c

Please sign in to comment.