X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/93d4b2d7eeb217c3369c548e2477bdc61aa16e80..0e63c25fcf834e2e18cecec6673e7cdecf8b4507:/Parser.ypp.in diff --git a/Parser.ypp.in b/Parser.ypp.in index aeafb35..687218a 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -304,6 +304,7 @@ type; }) %token Question "?" %token SemiColon ";" %token NewLine "\n" +%token __ "" %token Comment @@ -439,7 +440,6 @@ type; }) %token _SEL_ "SEL" @end -%token _auto_ "auto" %token _each_ "each" %token _of_ "of" %token _of__ "!of" @@ -450,7 +450,7 @@ type; }) @end %token AutoComplete -%token YieldStar +%token YieldStar "yield *" %token Identifier_ %token NumericLiteral @@ -630,7 +630,9 @@ type; }) %type TypeSignifier %type TypeQualifierLeft %type TypeQualifierRight -%type TypedIdentifier +%type TypedIdentifierMaybe +%type TypedIdentifierNo +%type TypedIdentifierYes %type TypedParameterList_ %type TypedParameterList %type TypedParameterListOpt @@ -720,10 +722,14 @@ LexPushYieldOn: { driver.yield_.push(true); }; LexPushYieldOff: { driver.yield_.push(false); }; LexPopYield: { driver.yield_.pop(); }; -LexNewLine +LexNewLineOrOpt : { CYLEX(); if (driver.hold_ != empty_symbol) CYERR(@$, "unexpected hold"); if (driver.newline_) { driver.hold_ = yyla.type; yyla.type = yytranslate_(cy::parser::token::NewLine); } } ; +LexNewLineOrNot + : { CYLEX(); if (driver.hold_ != empty_symbol) CYERR(@$, "unexpected hold"); driver.hold_ = yyla.type; yyla.type = yytranslate_(driver.newline_ ? cy::parser::token::NewLine : cy::parser::token::__); } + ; + LexNoStar : { CYMAP(YieldStar, Star); } ; @@ -760,7 +766,6 @@ IdentifierName Word : IdentifierNoOf[pass] { $$ = $pass; } - | "auto" { $$ = CYNew CYWord("auto"); } | "break" { $$ = CYNew CYWord("break"); } | "case" { $$ = CYNew CYWord("case"); } | "catch" { $$ = CYNew CYWord("catch"); } @@ -834,9 +839,18 @@ StrictSemi : { driver.Warning(@$, "warning, automatic semi-colon insertion required"); } ; +NewLineNot + : LexNewLineOrNot "" + ; + +NewLineOpt + : LexNewLineOrNot "\n" + | NewLineNot + ; + TerminatorSoft - : ";" - | "\n" StrictSemi + : LexNewLineOrNot "\n" StrictSemi + | NewLineNot LexOf Terminator ; Terminator @@ -926,9 +940,7 @@ IdentifierNoOf | "static" { $$ = CYNew CYIdentifier("static"); } | "volatile" { $$ = CYNew CYIdentifier("volatile"); } @begin C - | "extern" { $$ = CYNew CYIdentifier("extern"); } | "signed" { $$ = CYNew CYIdentifier("signed"); } - | "typedef" { $$ = CYNew CYIdentifier("typedef"); } | "unsigned" { $$ = CYNew CYIdentifier("unsigned"); } @end @begin ObjectiveC @@ -1049,12 +1061,12 @@ InitializerOpt /* 12.2.9 Template Literals {{{ */ TemplateLiteral : NoSubstitutionTemplate[string] { $$ = CYNew CYTemplate($string, NULL); } - | TemplateHead[string] TemplateSpans[spans] { $$ = CYNew CYTemplate($string, $spans); } + | TemplateHead[string] LexPushInOff TemplateSpans[spans] { $$ = CYNew CYTemplate($string, $spans); } ; TemplateSpans : Expression[value] TemplateMiddle[string] TemplateSpans[spans] { $$ = CYNew CYSpan($value, $string, $spans); } - | Expression[value] TemplateTail[string] { $$ = CYNew CYSpan($value, $string, NULL); } + | Expression[value] TemplateTail[string] LexPopIn { $$ = CYNew CYSpan($value, $string, NULL); } ; /* }}} */ @@ -1132,15 +1144,15 @@ AccessExpression ; LeftHandSideExpression - : AccessExpression[pass] LexNewLine { $$ = $pass; } + : AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; } | IndirectExpression[pass] { $$ = $pass; } ; /* }}} */ /* 12.4 Postfix Expressions {{{ */ PostfixExpression - : AccessExpression[lhs] LexNewLine { $$ = $lhs; } - | AccessExpression[lhs] LexNewLine "++" { $$ = CYNew CYPostIncrement($lhs); } - | AccessExpression[lhs] LexNewLine "--" { $$ = CYNew CYPostDecrement($lhs); } + : AccessExpression[lhs] LexNewLineOrOpt { $$ = $lhs; } + | AccessExpression[lhs] LexNewLineOrOpt "++" { $$ = CYNew CYPostIncrement($lhs); } + | AccessExpression[lhs] LexNewLineOrOpt "--" { $$ = CYNew CYPostDecrement($lhs); } ; /* }}} */ /* 12.5 Unary Operators {{{ */ @@ -1527,7 +1539,7 @@ ForStatementInitializer ; ForInStatementInitializer - : LexLet LexOf AccessExpression[pass] LexNewLine { $$ = $pass; } + : LexLet LexOf AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; } | LexLet LexOf IndirectExpression[pass] { $$ = $pass; } | LexLet LexOf Var_ LexBind ForBinding[binding] { $$ = CYNew CYForVariable($binding); } | ForDeclaration[pass] { $$ = $pass; } @@ -1544,14 +1556,14 @@ ForBinding /* }}} */ /* 13.8 The continue Statement {{{ */ ContinueStatement - : "continue" LexNewLine TerminatorSoft { $$ = CYNew CYContinue(NULL); } - | "continue" LexNewLine Identifier[label] Terminator { $$ = CYNew CYContinue($label); } + : "continue" TerminatorSoft { $$ = CYNew CYContinue(NULL); } + | "continue" NewLineNot LexOf Identifier[label] Terminator { $$ = CYNew CYContinue($label); } ; /* }}} */ /* 13.9 The break Statement {{{ */ BreakStatement - : "break" LexNewLine TerminatorSoft { $$ = CYNew CYBreak(NULL); } - | "break" LexNewLine Identifier[label] Terminator { $$ = CYNew CYBreak($label); } + : "break" TerminatorSoft { $$ = CYNew CYBreak(NULL); } + | "break" NewLineNot LexOf Identifier[label] Terminator { $$ = CYNew CYBreak($label); } ; /* }}} */ /* 13.10 The return Statement {{{ */ @@ -1560,8 +1572,8 @@ Return ; ReturnStatement - : Return LexNewLine LexOf TerminatorSoft { $$ = CYNew CYReturn(NULL); } - | Return LexNewLine Expression[value] Terminator { $$ = CYNew CYReturn($value); } + : Return TerminatorSoft { $$ = CYNew CYReturn(NULL); } + | Return NewLineNot Expression[value] Terminator { $$ = CYNew CYReturn($value); } ; /* }}} */ /* 13.11 The with Statement {{{ */ @@ -1605,8 +1617,8 @@ LabelledItem /* }}} */ /* 13.14 The throw Statement {{{ */ ThrowStatement - : "throw"[throw] LexNewLine LexOf TerminatorSoft { CYERR(@throw, "throw without exception"); } - | "throw" LexNewLine Expression[value] Terminator { $$ = CYNew cy::Syntax::Throw($value); } + : "throw"[throw] TerminatorSoft { CYERR(@throw, "throw without exception"); } + | "throw" NewLineNot Expression[value] Terminator { $$ = CYNew cy::Syntax::Throw($value); } ; /* }}} */ /* 13.15 The try Statement {{{ */ @@ -1681,7 +1693,7 @@ FunctionStatementList /* }}} */ /* 14.2 Arrow Function Definitions {{{ */ ArrowFunction - : ArrowParameters[parameters] LexNewLine "=>" LexNoBrace ConciseBody[code] { $$ = CYNew CYFatArrow($parameters, $code); } + : ArrowParameters[parameters] LexNewLineOrOpt "=>" LexNoBrace ConciseBody[code] { $$ = CYNew CYFatArrow($parameters, $code); } ; ArrowParameters @@ -1723,15 +1735,11 @@ GeneratorBody : LexPushYieldOn FunctionStatementList[code] LexPopYield { $$ = $code; } ; -Yield - : "!yield" LexNewLine LexNoStar - ; - YieldExpression - : Yield LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } - | Yield LexOf "\n" { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } - | Yield AssignmentExpression[value] { CYNOT(@$); /* $$ = CYNew CYYieldValue($value); */ } - | Yield LexOf YieldStar AssignmentExpression[generator] { CYNOT(@$); /* $$ = CYNew CYYieldGenerator($generator); */ } + : "!yield" LexNewLineOrNot "\n" LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } + | "!yield" LexNewLineOrNot "" LexNoStar LexOf { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } + | "!yield" LexNewLineOrNot "" LexNoStar AssignmentExpression[value] { CYNOT(@$); /* $$ = CYNew CYYieldValue($value); */ } + | "!yield" LexNewLineOrNot "" LexNoStar LexOf "yield *" AssignmentExpression[generator] { CYNOT(@$); /* $$ = CYNew CYYieldGenerator($generator); */ } ; /* }}} */ /* 14.5 Class Definitions {{{ */ @@ -1976,12 +1984,20 @@ PrimitiveType | "unsigned" "char" { $$ = CYNew CYTypeUnsigned(CYNew CYTypeVariable("char")); } ; -TypedIdentifier +TypedIdentifierMaybe : TypeQualifierLeft[modifier] PrimitiveType[specifier] TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = $specifier; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } ; +TypedIdentifierYes + : TypedIdentifierMaybe[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); $$ = $typed; } + ; + +TypedIdentifierNo + : TypedIdentifierMaybe[typed] { if ($typed->identifier_ != NULL) CYERR($typed->location_, "unexpected identifier"); $$ = $typed; } + ; + PrimaryExpression - : "@encode" "(" TypedIdentifier[typed] ")" { $$ = CYNew CYEncodedType($typed); } + : "@encode" "(" TypedIdentifierMaybe[typed] ")" { $$ = CYNew CYEncodedType($typed); } ; /* }}} */ @end @@ -1995,7 +2011,7 @@ ClassSuperOpt ; ImplementationFieldListOpt - : TypedIdentifier[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } + : TypedIdentifierMaybe[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } | { $$ = NULL; } ; @@ -2009,7 +2025,7 @@ MessageScope ; TypeOpt - : "(" TypedIdentifier[type] ")" { if ($type->identifier_ != NULL) CYERR($type->location_, "unexpected identifier"); $$ = $type; } + : "(" TypedIdentifierNo[type] ")" { $$ = $type; } | { $$ = CYNew CYTypedIdentifier(CYNew CYTypeVariable("id")); } ; @@ -2159,7 +2175,7 @@ PrimaryExpression /* }}} */ /* Cycript (Objective-C): Block Expressions {{{ */ PrimaryExpression - : "^" TypedIdentifier[type] { if ($type->identifier_ != NULL) CYERR($type->location_, "unexpected identifier"); } "{" FunctionBody[code] "}" { if (CYTypeFunctionWith *function = $type->Function()) $$ = CYNew CYObjCBlock($type, function->parameters_, $code); else CYERR($type->location_, "expected parameters"); } + : "^" TypedIdentifierNo[type] "{" FunctionBody[code] "}" { if (CYTypeFunctionWith *function = $type->Function()) $$ = CYNew CYObjCBlock($type, function->parameters_, $code); else CYERR($type->location_, "expected parameters"); } ; /* }}} */ /* Cycript (Objective-C): Instance Literals {{{ */ @@ -2189,11 +2205,6 @@ MemberAccess | "->" AutoComplete { driver.mode_ = CYDriver::AutoIndirect; YYACCEPT; } ; /* }}} */ -/* Cycript (C): auto Compatibility {{{ */ -Var_ - : "auto" - ; -/* }}} */ /* Cycript (C): Lambda Expressions {{{ */ TypedParameterList_ : "," TypedParameterList[parameters] { $$ = $parameters; } @@ -2201,7 +2212,7 @@ TypedParameterList_ ; TypedParameterList - : TypedIdentifier[typed] TypedParameterList_[next] { $$ = CYNew CYTypedParameter($typed, $next); } + : TypedIdentifierMaybe[typed] TypedParameterList_[next] { $$ = CYNew CYTypedParameter($typed, $next); } ; TypedParameterListOpt @@ -2210,17 +2221,29 @@ TypedParameterListOpt ; PrimaryExpression - : "[" LexOf "&" "]" "(" TypedParameterListOpt[parameters] ")" "->" TypedIdentifier[type] "{" FunctionBody[code] "}" { $$ = CYNew CYLambda($type, $parameters, $code); } + : "[" LexOf "&" "]" "(" TypedParameterListOpt[parameters] ")" "->" TypedIdentifierMaybe[type] "{" FunctionBody[code] "}" { $$ = CYNew CYLambda($type, $parameters, $code); } ; /* }}} */ /* Cycript (C): Type Definitions {{{ */ +IdentifierNoOf + : "typedef" NewLineOpt { $$ = CYNew CYIdentifier("typedef"); } + ; + Statement__ - : "typedef" TypedIdentifier[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); } Terminator { $$ = CYNew CYTypeDefinition($typed); } + : "typedef" NewLineNot TypedIdentifierYes[typed] Terminator { $$ = CYNew CYTypeDefinition($typed); } + ; + +PrimaryExpression + : "(" LexOf "typedef" NewLineOpt TypedIdentifierNo[typed] ")" { $$ = CYNew CYTypeExpression($typed); } ; /* }}} */ /* Cycript (C): extern "C" {{{ */ +IdentifierNoOf + : "extern" NewLineOpt { $$ = CYNew CYIdentifier("extern"); } + ; + Statement__ - : "extern" StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } TypedIdentifier[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); } Terminator { $$ = CYNew CYExternal($abi, $typed); } + : "extern" NewLineNot StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } TypedIdentifierYes[typed] Terminator { $$ = CYNew CYExternal($abi, $typed); } ; /* }}} */ @@ -2483,7 +2506,7 @@ PrimaryExpression ; PostfixExpression - : PostfixExpression[lhs] RubyProcExpression[rhs] LexNewLine { $$ = CYNew CYRubyBlock($lhs, $rhs); } + : PostfixExpression[lhs] RubyProcExpression[rhs] LexNewLineOrOpt { $$ = CYNew CYRubyBlock($lhs, $rhs); } ; /* }}} */