-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #570 from goby-lang/namespace-parser-constants
Namespace parser constants
- Loading branch information
Showing
12 changed files
with
300 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.