Skip to content

Commit

Permalink
Parser passes all tests plus a large smokescreen test from SproutCore
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Oct 23, 2011
1 parent e96656c commit 42b692d
Show file tree
Hide file tree
Showing 4 changed files with 577 additions and 67 deletions.
12 changes: 12 additions & 0 deletions lib/lattescript/ast.rb
Expand Up @@ -8,6 +8,10 @@ def cuddly?
def statement?
false
end

def needs_newline?
false
end
end

class SequenceExpression
Expand All @@ -21,6 +25,10 @@ def cuddle!
@cuddly = true
end

def needs_newline?
!@cuddly
end

def cuddly?
true
end
Expand All @@ -38,6 +46,10 @@ def statement?

statements.each do |statement|
statement.class_eval do
def needs_newline?
true
end

def statement?
true
end
Expand Down
67 changes: 38 additions & 29 deletions lib/lattescript/grammar.kpeg
Expand Up @@ -8,22 +8,22 @@
%% identifier = ast Identifier(name)
%% literal = ast Literal(val)
%% number = ast Number(val)
%% string = ast String(val)
%% string = ast String(val, quote)
%% this_expression = ast ThisExpression()
%% variable_declaration = ast VariableDeclaration(kind, declarations)
%% variable_declaration = ast VariableDeclaration(kind, declarations, semicolon)
%% variable_declarator = ast VariableDeclarator(id, init)
%% array_expression = ast ArrayExpression(elements)
%% object_expression = ast ObjectExpression(properties)
%% function_declaration = ast FunctionDeclaration(id, params, body)
%% function_expression = ast FunctionDeclaration(id, params, body)
%% function_expression = ast FunctionExpression(id, params, body)
%% return_statement = ast ReturnStatement(argument)
%% try_statement = ast TryStatement(block, handler, finalizer)
%% catch_clause = ast CatchClause(param, guard, body)
%% catch_clause = ast CatchClause(param, body)
%% throw_statement = ast ThrowStatement(argument)
%% labeled_statement = ast LabeledStatement(label, body)
%% break_statement = ast BreakStatement(label)
%% continue_statement = ast ContinueStatement(label)
%% switch_statement = ast SwitchStatement(discriminant, cases, lexical)
%% switch_statement = ast SwitchStatement(discriminant, cases)
%% switch_case = ast SwitchCase(test, consequent)
%% with_statement = ast WithStatement(object, body)
%% conditional_expression = ast ConditionalExpression(test, consequent, alternate)
Expand All @@ -46,12 +46,13 @@
%% object_pattern = ast ObjectPattern(properties)
%% array_pattern = ast ArrayPattern(elements)
%% spread = ast Spread(name)
%% comment = ast Comment(body, type)
%% comment = ast Comment(body, type, newline)

# Whitespace and Source Characters

S = WhiteSpace | LineTerminatorSequence | Comment
- = S*
SnoComment = WhiteSpace | LineTerminatorSequence

SnoLB = (WhiteSpace | SingleLineComment | MultiLineCommentNoLB)+

Expand All @@ -64,9 +65,9 @@ LineTerminator = LF | CR
LineTerminatorSequence = LF | CR LF | CR

Comment = MultiLineComment | SingleLineComment
MultiLineComment = "/*" < (!("*/") SourceCharacter)* > "*/" ~comment(text, 'multiline')
MultiLineCommentNoLB = "/*" < (!("*/") !(LineTerminator) SourceCharacter)* > "*/" ~comment(text, 'multiline')
SingleLineComment = "//" /[^\n\r]*/
MultiLineComment = "/*" < (!("*/") SourceCharacter)* > "*/" LineTerminatorSequence?:lf ~comment(text, 'multiline', lf)
MultiLineCommentNoLB = "/*" < (!("*/") !(LineTerminator) SourceCharacter)* > "*/" LineTerminatorSequence?:lf ~comment(text, 'multiline', lf)
SingleLineComment = "//" < /[^\n\r]*/ > ~comment(text, 'singleline', false)

SourceCharacter = /[\u0000-\u10FFFF]/

Expand Down Expand Up @@ -131,14 +132,13 @@ WithTok = "with" !(IdentifierPart)

root = Program:p { p }

Program = (- (Statement | FunctionDeclaration | Comment))*:s - ~program(s)
Program = (SnoComment* (Statement | FunctionDeclaration))*:s - ~program(s)

FunctionBody = (- (Statement | FunctionDeclaration))*:statements - { statements }
FunctionBody = (SnoComment* (Statement | FunctionDeclaration))*:statements SnoComment* { statements }

FunctionDeclaration = FunctionTok - Identifier:id - "(" - FormalParameterList?:params - ")" - "{" - FunctionBody:body - "}" ~function_declaration(id, params, body)
FunctionDeclaration = FunctionTok - Identifier:id - "(" - FormalParameterList?:params - ")" - "{" SnoComment* FunctionBody:body SnoComment* "}" ~function_declaration(id, params || [], body)

FunctionExpression = FunctionTok - Identifier?:id - "(" - FormalParameterList?:params - ")" - "{" - FunctionBody:body - "}"
~function_expression(id, params || [], body)
FunctionExpression = FunctionTok - Identifier?:id - "(" - FormalParameterList?:params - ")" - "{" SnoComment* FunctionBody:body SnoComment* "}" ~function_expression(id, params || [], body)

FormalParameterList = Identifier:id ("," - Identifier)*:ids { [id] + ids }

Expand All @@ -147,7 +147,8 @@ UseStrictDirective = "use" S "strict" S ("," !(LineTerminator) SourceCharacter)*
# Statements

Statement =
Block
Comment
| Block
| VariableStatement
| EmptyStatement
| ExpressionStatement
Expand All @@ -163,9 +164,9 @@ Statement =
| TryStatement
| DebuggerStatement

Block = "{" (- Statement)*:statements - "}" ~block_statement(statements)
Block = "{" (SnoComment* Statement)*:statements SnoComment* "}" ~block_statement(statements)

VariableStatement = VarTok - VariableDeclaration:decl (- "," - VariableDeclaration)*:decls EOS ~variable_declaration("var", [decl] + decls)
VariableStatement = VarTok - VariableDeclaration:decl (- "," - VariableDeclaration)*:decls EOS ~variable_declaration("var", [decl] + decls, true)

VariableDeclaration = Identifier:id (- "=" !("=") - AssignmentExpression:expr)? ~variable_declarator(id, expr)

Expand All @@ -191,18 +192,19 @@ ForInStatement =
ForTok - "(" (ForInLeft|ForInVarLeft):left InTok - Expression:right - ")" - Statement:stmt ~for_in_statement(left, right, stmt, nil)

ForInLeft = - LeftHandSideExpression -
ForInVarLeft = - VarTok - VariableDeclarationNoIn:decl - ~variable_declaration("var", [decl])
ForInVarLeft = - VarTok - VariableDeclarationNoIn:decl - ~variable_declaration("var", [decl], false)

ForStatement =
ForTok - "(" - (ForInit|ForVarInit)?:init - ";" - ForTest?:test - ";" - ForUpdate?:update - ")" - Statement:body ~for_statement(init, test, update, body)

ForInit = ExpressionNoIn
ForVarInit = VarTok - VariableDeclarationListNoIn:list ~variable_declaration("var", list)
ForVarInit = VarTok - VariableDeclarationListNoIn:list ~variable_declaration("var", list, false)
ForTest = Expression
ForUpdate = Expression

ContinueStatement =
ContinueTok SnoLB? (Identifier:id EOS | EOSnoLB) ~continue_statement(id)
ContinueTok SnoLB? Identifier:id EOS ~continue_statement(id)
| ContinueTok SnoLB? EOSnoLB ~continue_statement(nil)

BreakStatement =
BreakTok SnoLB? (Identifier:id EOS | EOSnoLB) ~break_statement(id)
Expand All @@ -217,20 +219,25 @@ LabeledStatement =
Identifier:id - ":" - Statement:statement ~labeled_statement(id, statement)

SwitchStatement =
SwitchTok - "(" - Expression:expr - ")" - "{" CaseClause*:clauses (DefaultClause:default CaseClause*:more_clauses)? - "}" # TODO
SwitchTok - "(" - Expression:expr - ")" - "{" - CaseClauses:clauses - "}" ~switch_statement(expr, clauses)

CaseClauses =
CaseClause*:clauses - DefaultClause:default - CaseClause*:more_clauses { clauses + [default] + more_clauses }
| CaseClause*:clauses - DefaultClause:default { clauses + [default] }
| CaseClause*:clauses { clauses }

CaseClause =
- CaseTok - Expression - ":" (- Statement)*
- CaseTok - Expression:expr - ":" (- Statement)*:statements ~switch_case(expr, statements)

DefaultClause =
- DefaultTok - ":" (- Statement)*
- DefaultTok:tok - ":" (- Statement)*:statements ~switch_case(nil, statements)

ThrowStatement =
ThrowTok SnoLB? (EOSnoLB | Expression:expr EOS) ~throw_statement(expr)

TryStatement =
TryTok - Block:try - Catch:catch - Finally?:finally ~try_statement(try, catch, finally)
TryTok - Block:try - Finally:finally ~try_statement(try, nil, finally)
TryTok - Block:try - Catch:catch - Finally?:finally ~try_statement(try, catch, finally)
| TryTok - Block:try - Finally:finally ~try_statement(try, nil, finally)

Catch =
CatchTok - "(" - Identifier:id - ")" - Block:block ~catch_clause(id, block)
Expand Down Expand Up @@ -398,9 +405,9 @@ NewExpression =
MemberExpression =
MemberExpression:left - BracketAccessor:right ~member_expression(left, right, true)
| MemberExpression:left - DotAccessor:right ~member_expression(left, right, false)
| NewTok - MemberExpression:expr - Arguments:arguments ~new_expression(expr, arguments)
| PrimaryExpression
| FunctionExpression
| NewTok - MemberExpression:expr Arguments:arguments ~new_expression(expr, arguments)

PrimaryExpression =
ThisTok ~this_expression()
Expand Down Expand Up @@ -441,7 +448,9 @@ PropertySetter =
"set" - PropertyName - "(" - PropertySetParameterList - ")" - "{" - FunctionBody - "}"

PropertyName =
IdentifierName | StringLiteral | NumericLiteral
IdentifierName:name ~identifier(name)
| StringLiteral
| NumericLiteral

PropertySetParameterList =
Identifier
Expand Down Expand Up @@ -483,8 +492,8 @@ SignedInteger = DecimalDigit+
DQ = "\""
SQ = "'"

StringLiteral = DQ < DoubleStringCharacter* > DQ ~string(text)
| SQ < SingleStringCharacter* > SQ ~string(text)
StringLiteral = DQ < DoubleStringCharacter* > DQ ~string(text, '"')
| SQ < SingleStringCharacter* > SQ ~string(text, "'")

RS = "\\"

Expand Down

0 comments on commit 42b692d

Please sign in to comment.