package parserimport ()const ( err_UnexpectedToken = "Unexpected token %v" err_UnexpectedEndOfInput = "Unexpected end of input" err_UnexpectedEscape = "Unexpected escape")// UnexpectedNumber: 'Unexpected number',// UnexpectedString: 'Unexpected string',// UnexpectedIdentifier: 'Unexpected identifier',// UnexpectedReserved: 'Unexpected reserved word',// NewlineAfterThrow: 'Illegal newline after throw',// InvalidRegExp: 'Invalid regular expression',// UnterminatedRegExp: 'Invalid regular expression: missing /',// InvalidLHSInAssignment: 'Invalid left-hand side in assignment',// InvalidLHSInForIn: 'Invalid left-hand side in for-in',// MultipleDefaultsInSwitch: 'More than one default clause in switch statement',// NoCatchOrFinally: 'Missing catch or finally after try',// UnknownLabel: 'Undefined label \'%0\'',// Redeclaration: '%0 \'%1\' has already been declared',// IllegalContinue: 'Illegal continue statement',// IllegalBreak: 'Illegal break statement',// IllegalReturn: 'Illegal return statement',// StrictModeWith: 'Strict mode code may not include a with statement',// StrictCatchVariable: 'Catch variable may not be eval or arguments in strict mode',// StrictVarName: 'Variable name may not be eval or arguments in strict mode',// StrictParamName: 'Parameter name eval or arguments is not allowed in strict mode',// StrictParamDupe: 'Strict mode function may not have duplicate parameter names',// StrictFunctionName: 'Function name may not be eval or arguments in strict mode',// StrictOctalLiteral: 'Octal literals are not allowed in strict mode.',// StrictDelete: 'Delete of an unqualified identifier in strict mode.',// StrictDuplicateProperty: 'Duplicate data property in object literal not allowed in strict mode',// AccessorDataProperty: 'Object literal may not have data and accessor property with the same name',// AccessorGetSet: 'Object literal may not have multiple get/set accessors with the same name',// StrictLHSAssignment: 'Assignment to eval or arguments is not allowed in strict mode',// StrictLHSPostfix: 'Postfix increment/decrement may not have eval or arguments operand in strict mode',// StrictLHSPrefix: 'Prefix increment/decrement may not have eval or arguments operand in strict mode',// StrictReservedWord: 'Use of future reserved word in strict mode'// A SyntaxError is a description of an ECMAScript syntax error.// An Error represents a parsing error. It includes the position where the error occurred and a message/description.typeErrorstruct { Position file.Position Message string}// FIXME Should this be "SyntaxError"?func ( Error) () string { := .Position.Filenameif == "" { = "(anonymous)" }returnfmt.Sprintf("%s: Line %d:%d %s", , .Position.Line, .Position.Column, .Message, )}func ( *_parser) ( interface{}, string, ...interface{}) *Error { := file.Idx(0)switch place := .(type) {caseint: = .idxOf()casefile.Idx:if == 0 { = .idxOf(.chrOffset) } else { = }default:panic(fmt.Errorf("error(%T, ...)", )) } := .position() = fmt.Sprintf(, ...) .errors.Add(, )return .errors[len(.errors)-1]}func ( *_parser) ( file.Idx, rune) error {if == -1 {return .error(, err_UnexpectedEndOfInput) }return .error(, err_UnexpectedToken, token.ILLEGAL)}func ( *_parser) ( token.Token) error {switch {casetoken.EOF:return .error(file.Idx(0), err_UnexpectedEndOfInput) } := .String()switch {casetoken.BOOLEAN, token.NULL: = .literalcasetoken.IDENTIFIER:return .error(.idx, "Unexpected identifier")casetoken.KEYWORD:// TODO Might be a future reserved wordreturn .error(.idx, "Unexpected reserved word")casetoken.ESCAPED_RESERVED_WORD:return .error(.idx, "Keyword must not contain escaped characters")casetoken.NUMBER:return .error(.idx, "Unexpected number")casetoken.STRING:return .error(.idx, "Unexpected string") }return .error(.idx, err_UnexpectedToken, )}// ErrorList is a list of *Errors.typeErrorList []*Error// Add adds an Error with given position and message to an ErrorList.func ( *ErrorList) ( file.Position, string) { * = append(*, &Error{, })}// Reset resets an ErrorList to no errors.func ( *ErrorList) () { * = (*)[0:0] }func ( ErrorList) () int { returnlen() }func ( ErrorList) (, int) { [], [] = [], [] }func ( ErrorList) (, int) bool { := &[].Position := &[].Positionif .Filename < .Filename {returntrue }if .Filename == .Filename {if .Line < .Line {returntrue }if .Line == .Line {return .Column < .Column } }returnfalse}func ( ErrorList) () {sort.Sort()}// Error implements the Error interface.func ( ErrorList) () string {switchlen() {case0:return"no errors"case1:return [0].Error() }returnfmt.Sprintf("%s (and %d more errors)", [0].Error(), len()-1)}// Err returns an error equivalent to this ErrorList.// If the list is empty, Err returns nil.func ( ErrorList) () error {iflen() == 0 {returnnil }return}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.