]> git.saurik.com Git - cycript.git/blobdiff - Cycript.y
Finished JS Bison/Flex parser.
[cycript.git] / Cycript.y
index 71da89ebf1e47a9977fa53faf22d7c26c059acff..f6ac4de9a71c868c00292f80711425051c0cda94 100644 (file)
--- a/Cycript.y
+++ b/Cycript.y
@@ -1,14 +1,19 @@
-%{
+%code top {
 #include "Parser.hpp"
 #include "Cycript.tab.h"
 void cyerror(YYLTYPE *locp, CYParser *context, const char *msg);
 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
-%}
+}
+
+%name-prefix "cy"
 
-%pure-parser
-%name-prefix="cy"
 %locations
+%define api.pure
+%glr-parser
+
 %defines
+
+%debug
 %error-verbose
 
 %parse-param { CYParser *context }
@@ -53,6 +58,18 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 %token CYTokenStarEqual "*="
 %token CYTokenTilde "~"
 
+%token CYTokenColon ":"
+%token CYTokenComma ","
+%token CYTokenQuestion "?"
+%token CYTokenSemiColon ";"
+
+%token CYTokenOpenParen "("
+%token CYTokenCloseParen ")"
+%token CYTokenOpenBrace "{"
+%token CYTokenCloseBrace "}"
+%token CYTokenOpenBracket "["
+%token CYTokenCloseBracket "]"
+
 %token CYTokenBreak "break"
 %token CYTokenCase "case"
 %token CYTokenCatch "catch"
@@ -82,9 +99,493 @@ int cylex(YYSTYPE *lvalp, YYLTYPE *llocp);
 %token CYTokenWhile "while"
 %token CYTokenWith "with"
 
+%token CYTokenIdentifier
+%token CYTokenNumber
+%token CYTokenString
+
 %%
 
-hello: ;
+Start
+    : Program
+    ;
+
+IdentifierOpt
+    : Identifier
+    |
+    ;
+
+Identifier
+    : CYTokenIdentifier
+    ;
+
+Literal
+    : NullLiteral
+    | BooleanLiteral
+    | NumericLiteral
+    | StringLiteral
+    ;
+
+NullLiteral
+    : "null"
+    ;
+
+BooleanLiteral
+    : "true"
+    | "false"
+    ;
+
+NumericLiteral
+    : CYTokenNumber
+    ;
+
+StringLiteral
+    : CYTokenString
+    ;
+
+/* Objective-C Extensions {{{ */
+VariadicCall
+    : "," AssignmentExpression VariadicCall
+    |
+    ;
+
+SelectorCall_
+    : SelectorCall
+    | VariadicCall
+    ;
+
+SelectorCall
+    : IdentifierOpt ":" AssignmentExpression SelectorCall_
+    ;
+
+SelectorList
+    : SelectorCall
+    | Identifier
+    ;
+
+ObjectiveCall
+    : "[" AssignmentExpression SelectorList "]"
+    ;
+/* }}} */
+
+/* 11.1 Primary Expressions {{{ */
+PrimaryExpression
+    : "this"
+    | Identifier
+    | Literal
+    | ArrayLiteral
+    | ObjectLiteral
+    | "(" Expression ")"
+    | ObjectiveCall
+    ;
+/* }}} */
+/* 11.1.4 Array Initialiser {{{ */
+ArrayLiteral
+    : "[" ElementList "]"
+    ;
+
+Element
+    : AssignmentExpression
+    |
+    ;
+
+ElementList_
+    : "," ElementList
+    |
+    ;
+
+ElementList
+    : Element ElementList_
+    ;
+/* }}} */
+/* 11.1.5 Object Initialiser {{{ */
+ObjectLiteral
+    : "{" PropertyNameAndValueListOpt "}"
+    ;
+
+PropertyNameAndValueList_
+    : "," PropertyNameAndValueList
+    |
+    ;
+
+PropertyNameAndValueListOpt
+    : PropertyNameAndValueList
+    |
+    ;
+
+PropertyNameAndValueList
+    : PropertyName ":" AssignmentExpression PropertyNameAndValueList_
+    ;
+
+PropertyName
+    : Identifier
+    | StringLiteral
+    | NumericLiteral
+    ;
+/* }}} */
+
+MemberExpression
+    : PrimaryExpression
+    | FunctionExpression
+    | MemberExpression "[" Expression "]"
+    | MemberExpression "." Identifier
+    | "new" MemberExpression Arguments
+    ;
+
+NewExpression
+    : MemberExpression
+    | "new" NewExpression
+    ;
+
+CallExpression
+    : MemberExpression Arguments
+    | CallExpression Arguments
+    | CallExpression "[" Expression "]"
+    | CallExpression "." Identifier
+    ;
+
+ArgumentList_
+    : "," ArgumentList
+    |
+    ;
+
+ArgumentListOpt
+    : ArgumentList
+    |
+    ;
+
+ArgumentList
+    : AssignmentExpression ArgumentList_
+    ;
+
+Arguments
+    : "(" ArgumentListOpt ")"
+    ;
+
+LeftHandSideExpression
+    : NewExpression
+    | CallExpression
+    ;
+
+PostfixExpression
+    : LeftHandSideExpression
+    | LeftHandSideExpression "++"
+    | LeftHandSideExpression "--"
+    ;
+
+UnaryExpression
+    : PostfixExpression
+    | "delete" UnaryExpression
+    | "void" UnaryExpression
+    | "typeof" UnaryExpression
+    | "++" UnaryExpression
+    | "--" UnaryExpression
+    | "+" UnaryExpression
+    | "-" UnaryExpression
+    | "~" UnaryExpression
+    | "!" UnaryExpression
+    | "*" UnaryExpression
+    | "&" UnaryExpression
+    ;
+
+MultiplicativeExpression
+    : UnaryExpression
+    | MultiplicativeExpression "*" UnaryExpression
+    | MultiplicativeExpression "/" UnaryExpression
+    | MultiplicativeExpression "%" UnaryExpression
+    ;
+
+AdditiveExpression
+    : MultiplicativeExpression
+    | AdditiveExpression "+" MultiplicativeExpression
+    | AdditiveExpression "-" MultiplicativeExpression
+    ;
+
+ShiftExpression
+    : AdditiveExpression
+    | ShiftExpression "<<" AdditiveExpression
+    | ShiftExpression ">>" AdditiveExpression
+    | ShiftExpression ">>>" AdditiveExpression
+    ;
+
+RelationalExpression
+    : ShiftExpression
+    | RelationalExpression "<" ShiftExpression
+    | RelationalExpression ">" ShiftExpression
+    | RelationalExpression "<=" ShiftExpression
+    | RelationalExpression ">=" ShiftExpression
+    | RelationalExpression "instanceof" ShiftExpression
+    | RelationalExpression "in" ShiftExpression
+    ;
+
+EqualityExpression
+    : RelationalExpression
+    | EqualityExpression "==" RelationalExpression
+    | EqualityExpression "!=" RelationalExpression
+    | EqualityExpression "===" RelationalExpression
+    | EqualityExpression "!==" RelationalExpression
+    ;
+
+BitwiseANDExpression
+    : EqualityExpression
+    | BitwiseANDExpression "&" EqualityExpression
+    ;
+
+BitwiseXORExpression
+    : BitwiseANDExpression
+    | BitwiseXORExpression "^" BitwiseANDExpression
+    ;
+
+BitwiseORExpression
+    : BitwiseXORExpression
+    | BitwiseORExpression "|" BitwiseXORExpression
+    ;
+
+LogicalANDExpression
+    : BitwiseORExpression
+    | LogicalANDExpression "&&" BitwiseORExpression
+    ;
+
+LogicalORExpression
+    : LogicalANDExpression
+    | LogicalORExpression "||" LogicalANDExpression
+    ;
+
+ConditionalExpression
+    : LogicalORExpression
+    | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression
+    ;
+
+AssignmentExpression
+    : ConditionalExpression
+    | LeftHandSideExpression AssignmentOperator AssignmentExpression
+    ;
+
+AssignmentOperator
+    : "="
+    | "*="
+    | "/="
+    | "%="
+    | "+="
+    | "-="
+    | "<<="
+    | ">>="
+    | ">>>="
+    | "&="
+    | "^="
+    | "|="
+    ;
+
+Expression_
+    : "," Expression
+    |
+    ;
+
+ExpressionOpt
+    : Expression
+    |
+    ;
+
+Expression
+    : AssignmentExpression Expression_
+    ;
+
+Statement
+    : Block
+    | VariableStatement
+    | EmptyStatement
+    | ExpressionStatement
+    | IfStatement
+    | IterationStatement
+    | ContinueStatement
+    | BreakStatement
+    | ReturnStatement
+    | WithStatement
+    | LabelledStatement
+    | SwitchStatement
+    | ThrowStatement
+    | TryStatement
+    ;
+
+Block
+    : "{" StatementListOpt "}"
+    ;
+
+StatementListOpt
+    : Statement StatementListOpt
+    |
+    ;
+
+VariableStatement
+    : "var" VariableDeclarationList ";"
+    ;
+
+VariableDeclarationList_
+    : "," VariableDeclarationList
+    |
+    ;
+
+VariableDeclarationList
+    : VariableDeclaration VariableDeclarationList_
+    ;
+
+VariableDeclaration
+    : Identifier InitialiserOpt
+    ;
+
+InitialiserOpt
+    : Initialiser
+    |
+    ;
+
+Initialiser
+    : "=" AssignmentExpression
+    ;
+
+EmptyStatement
+    : ";"
+    ;
+
+ExpressionStatement
+    : Expression ";"
+    ;
+
+ElseStatementOpt
+    : "else" Statement
+    |
+    ;
+
+IfStatement
+    : "if" "(" Expression ")" Statement ElseStatementOpt
+    ;
+
+IterationStatement
+    : DoWhileStatement
+    | WhileStatement
+    | ForStatement
+    | ForInStatement
+    ;
+
+DoWhileStatement
+    : "do" Statement "while" "(" Expression ")" ";"
+    ;
+
+WhileStatement
+    : "while" "(" Expression ")" Statement
+    ;
+
+ForStatement
+    : "for" "(" ForStatementInitialiser ";" ExpressionOpt ";" ExpressionOpt ")" Statement
+    ;
+
+ForStatementInitialiser
+    : ExpressionOpt
+    | "var" VariableDeclarationList
+    ;
+
+ForInStatement
+    : "for" "(" ForInStatementInitialiser "in" Expression ")" Statement
+    ;
+
+ForInStatementInitialiser
+    : LeftHandSideExpression
+    | "var" VariableDeclaration
+    ;
+
+ContinueStatement
+    : "continue" IdentifierOpt ";"
+    ;
+
+BreakStatement
+    : "break" IdentifierOpt ";"
+    ;
+
+ReturnStatement
+    : "return" ExpressionOpt ";"
+    ;
+
+WithStatement
+    : "with" "(" Expression ")" Statement
+    ;
+
+SwitchStatement
+    : "switch" "(" Expression ")" CaseBlock
+    ;
+
+CaseBlock
+    : "{" CaseClausesOpt "}"
+    ;
+
+CaseClausesOpt
+    : CaseClause CaseClausesOpt
+    | DefaultClause CaseClausesOpt
+    |
+    ;
+
+CaseClause
+    : "case" Expression ":" StatementListOpt
+    ;
+
+DefaultClause
+    : "default" ":" StatementListOpt
+    ;
+
+LabelledStatement
+    : Identifier ":" Statement
+    ;
+
+ThrowStatement
+    : "throw" Expression ";"
+    ;
+
+TryStatement
+    : "try" Block CatchOpt FinallyOpt
+    ;
+
+CatchOpt
+    : "catch" "(" Identifier ")" Block
+    |
+    ;
+
+FinallyOpt
+    : "finally" Block
+    |
+    ;
+
+FunctionDeclaration
+    : "function" Identifier "(" FormalParameterList ")" "{" FunctionBody "}"
+    ;
+
+FunctionExpression
+    : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}"
+    ;
+
+FormalParameterList_
+    : "," FormalParameterList
+    |
+    ;
+
+FormalParameterList
+    : Identifier FormalParameterList_
+    |
+    ;
+
+FunctionBody
+    : SourceElements
+    ;
+
+Program
+    : SourceElements
+    ;
+
+SourceElements
+    : SourceElement SourceElements
+    |
+    ;
+
+SourceElement
+    : Statement
+    | FunctionDeclaration
+    ;
 
 %%