X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/72b2086fb92dfcc524adc4749395c1e06b2594ac..5a6c975adbe2588a10190cba75e9152682bedeae:/Parser.ypp.in diff --git a/Parser.ypp.in b/Parser.ypp.in index 9fe2a73..acda20a 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -61,6 +61,7 @@ %union { CYForInInitializer *forin_; } %union { CYFunctionParameter *functionParameter_; } %union { CYIdentifier *identifier_; } +%union { CYImportSpecifier *import_; } %union { CYInfix *infix_; } %union { CYLiteral *literal_; } %union { CYMethod *method_; } @@ -80,6 +81,8 @@ %union { CYWord *word_; } @begin C +%union { CYTypeIntegral *integral_; } +%union { CYTypeStructField *structField_; } %union { CYTypeModifier *modifier_; } %union { CYTypeSpecifier *specifier_; } %union { CYTypedIdentifier *typedIdentifier_; } @@ -87,9 +90,10 @@ @end @begin ObjectiveC +%union { CYObjCKeyValue *keyValue_; } +%union { CYImplementationField *implementationField_; } %union { CYMessage *message_; } %union { CYMessageParameter *messageParameter_; } -%union { CYImplementationField *implementationField_; } %union { CYProtocol *protocol_; } %union { CYSelectorPart *selector_; } @end @@ -304,6 +308,7 @@ type; }) %token Question "?" %token SemiColon ";" %token NewLine "\n" +%token __ "" %token Comment @@ -329,6 +334,7 @@ type; }) %token _typedef_ "typedef" %token _unsigned_ "unsigned" %token _signed_ "signed" +%token _struct_ "struct" %token _extern_ "extern" @end @@ -354,6 +360,7 @@ type; }) %token _null_ "null" %token _true_ "true" +%token _as_ "as" %token _break_ "break" %token _case_ "case" %token _catch_ "catch" @@ -416,8 +423,11 @@ type; }) %token _package_ "package" %token _private_ "private" %token _protected_ "protected" +%token ___proto___ "__proto__" %token _prototype_ "prototype" %token _public_ "public" +%token ___restrict_ "__restrict" +%token _restrict_ "restrict" %token _set_ "set" %token _short_ "short" %token _static_ "static" @@ -449,7 +459,7 @@ type; }) @end %token AutoComplete -%token YieldStar +%token YieldStar "yield *" %token Identifier_ %token NumericLiteral @@ -468,11 +478,11 @@ type; }) %type ArgumentListOpt %type Arguments %type ArrayComprehension +%type ArrayElement %type ArrayLiteral %type ArrowFunction %type ArrowParameters %type AssignmentExpression -%type AssignmentExpressionOpt %type BindingIdentifier %type BindingIdentifierOpt %type BindingList_ @@ -511,6 +521,7 @@ type; }) %type Declaration_ %type Declaration %type DefaultClause +%type ElementList_ %type ElementList %type ElementListOpt %type ElseStatementOpt @@ -520,6 +531,9 @@ type; }) %type ExpressionOpt %type ExpressionStatement_ %type ExpressionStatement +%type ExternC +%type ExternCStatement +%type ExternCStatementListOpt %type Finally %type ForBinding %type ForDeclaration @@ -529,6 +543,7 @@ type; }) %type FormalParameterList_ %type FormalParameterList %type FormalParameters +%type FromClause %type FunctionBody %type FunctionDeclaration %type FunctionExpression @@ -542,9 +557,18 @@ type; }) %type IdentifierNoOf %type IdentifierType %type IdentifierTypeNoOf +%type IdentifierTypeOpt %type IdentifierName %type IdentifierReference %type IfStatement +%type ImportClause +%type ImportDeclaration +%type ImportSpecifier +%type ImportedBinding +%type ImportedDefaultBinding +%type ImportsList_ +%type ImportsList +%type ImportsListOpt %type IndirectExpression %type Initializer %type InitializerOpt @@ -565,8 +589,16 @@ type; }) %type MemberAccess %type MemberExpression %type MethodDefinition +%type ModuleBody +%type ModuleBodyOpt +%type ModuleItem +%type ModuleItemList +%type ModuleItemListOpt %type ModulePath +%type ModuleSpecifier %type MultiplicativeExpression +%type NameSpaceImport +%type NamedImports %type NewExpression %type NullLiteral %type ObjectLiteral @@ -582,6 +614,8 @@ type; }) %type RegularExpressionSlash %type RelationalExpression %type ReturnStatement +%type RubyBlockExpression_ +%type RubyBlockExpression %type RubyProcExpression %type RubyProcParameterList_ %type RubyProcParameterList @@ -606,6 +640,7 @@ type; }) %type TemplateSpans %type ThrowStatement %type TryStatement +%type TypeDefinition %type UnaryExpression_ %type UnaryExpression %type VariableDeclaration @@ -621,15 +656,27 @@ type; }) %type YieldExpression @begin C -%type IntegerType -%type IntegerTypeOpt +%type IntegerType +%type IntegerTypeOpt %type PrefixedType %type PrimitiveType +%type StructFieldListOpt %type SuffixedType +%type SuffixedTypeOpt %type TypeSignifier +%type TypeSignifierNone +%type TypeSignifierOpt +%type ParameterTail %type TypeQualifierLeft +%type TypeQualifierLeftOpt %type TypeQualifierRight -%type TypedIdentifier +%type TypeQualifierRightOpt +%type TypedIdentifierDefinition +%type TypedIdentifierEncoding +%type TypedIdentifierField +%type TypedIdentifierMaybe +%type TypedIdentifierNo +%type TypedIdentifierYes %type TypedParameterList_ %type TypedParameterList %type TypedParameterListOpt @@ -641,14 +688,16 @@ type; }) %type CategoryStatement %type ClassSuperOpt %type ConditionalExpressionClassic -%type ImplementationFieldListOpt -%type ImplementationFields %type ClassMessageDeclaration %type ClassMessageDeclarationListOpt %type ClassProtocolListOpt %type ClassProtocols %type ClassProtocolsOpt +%type ImplementationFieldListOpt %type ImplementationStatement +%type KeyValuePairList_ +%type KeyValuePairList +%type KeyValuePairListOpt %type MessageExpression %type MessageParameter %type MessageParameters @@ -710,19 +759,25 @@ LexPopIn: { driver.in_.pop(); }; LexPushReturnOn: { driver.return_.push(true); }; LexPopReturn: { driver.return_.pop(); }; +Return: "return"[return] { if (!driver.return_.top()) CYERR(@return, "invalid return"); }; LexPushSuperOn: { driver.super_.push(true); }; LexPushSuperOff: { driver.super_.push(false); }; LexPopSuper: { driver.super_.pop(); }; +Super: "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); }; 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); } ; @@ -832,9 +887,23 @@ StrictSemi : { driver.Warning(@$, "warning, automatic semi-colon insertion required"); } ; +NewLineNot + : LexNewLineOrNot "" + ; + +NewLineOpt + : LexNewLineOrNot "\n" + | NewLineNot + ; + TerminatorSoft + : LexNewLineOrNot "\n" StrictSemi + | NewLineNot LexOf Terminator + ; + +TerminatorHard : ";" - | "\n" StrictSemi + | error { if (yyla.type_get() != yyeof_) CYERR(@error, "required semi-colon"); else CYEOK(); } StrictSemi ; Terminator @@ -873,6 +942,7 @@ LabelIdentifier IdentifierTypeNoOf : Identifier_[pass] { $$ = $pass; } | "abstract" { $$ = CYNew CYIdentifier("abstract"); } + | "as" { $$ = CYNew CYIdentifier("as"); } | "await" { $$ = CYNew CYIdentifier("await"); } | "boolean" { $$ = CYNew CYIdentifier("boolean"); } | "byte" { $$ = CYNew CYIdentifier("byte"); } @@ -894,6 +964,7 @@ IdentifierTypeNoOf | "package" { $$ = CYNew CYIdentifier("package"); } | "private" { $$ = CYNew CYIdentifier("private"); } | "protected" { $$ = CYNew CYIdentifier("protected"); } + | "__proto__" { $$ = CYNew CYIdentifier("__proto__"); } | "prototype" { $$ = CYNew CYIdentifier("prototype"); } | "public" { $$ = CYNew CYIdentifier("public"); } | "set" { $$ = CYNew CYIdentifier("set"); } @@ -915,18 +986,23 @@ IdentifierType | "of" { $$ = CYNew CYIdentifier("of"); } ; +IdentifierTypeOpt + : IdentifierType[pass] { $$ = $pass; } + | { $$ = NULL; } + ; + IdentifierNoOf : IdentifierTypeNoOf | "char" { $$ = CYNew CYIdentifier("char"); } | "int" { $$ = CYNew CYIdentifier("int"); } | "long" { $$ = CYNew CYIdentifier("long"); } + | "__restrict" { $$ = CYNew CYIdentifier("__restrict"); } + | "restrict" { $$ = CYNew CYIdentifier("restrict"); } | "short" { $$ = CYNew CYIdentifier("short"); } | "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 @@ -979,10 +1055,19 @@ ArrayLiteral : "[" ElementListOpt[elements] "]" { $$ = CYNew CYArray($elements); } ; -ElementList - : AssignmentExpressionOpt[value] "," ElementListOpt[next] { $$ = CYNew CYElementValue($value, $next); } +ArrayElement + : AssignmentExpression[value] { $$ = CYNew CYElementValue($value); } | LexOf "..." AssignmentExpression[values] { $$ = CYNew CYElementSpread($values); } - | AssignmentExpression[value] { $$ = CYNew CYElementValue($value, NULL); } + ; + +ElementList_ + : "," ElementListOpt[elements] { $$ = $elements; } + | { $$ = NULL; } + ; + +ElementList + : ArrayElement[element] ElementList_[next] { $$ = $element; $$->SetNext($next); } + | LexOf "," ElementListOpt[next] { $$ = CYNew CYElementValue(NULL, $next); } ; ElementListOpt @@ -1073,8 +1158,8 @@ MemberExpression ; SuperProperty - : "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } "[" Expression[property] "]" { $$ = CYNew CYSuperAccess($property); } - | "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } "." IdentifierName[property] { $$ = CYNew CYSuperAccess(CYNew CYString($property)); } + : Super "[" Expression[property] "]" { $$ = CYNew CYSuperAccess($property); } + | Super "." IdentifierName[property] { $$ = CYNew CYSuperAccess(CYNew CYString($property)); } ; MetaProperty @@ -1102,7 +1187,7 @@ CallExpression ; SuperCall - : "super"[super] { if (!driver.super_.top()) CYERR(@super, "invalid super"); } Arguments[arguments] { $$ = CYNew CYSuperCall($arguments); } + : Super Arguments[arguments] { $$ = CYNew CYSuperCall($arguments); } ; Arguments @@ -1130,15 +1215,15 @@ AccessExpression ; LeftHandSideExpression - : AccessExpression[pass] LexNewLine { $$ = $pass; } + : RubyBlockExpression[pass] { $$ = $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); } + : RubyBlockExpression[pass] { $$ = $pass; } + | AccessExpression[lhs] LexNewLineOrOpt "++" { $$ = CYNew CYPostIncrement($lhs); } + | AccessExpression[lhs] LexNewLineOrOpt "--" { $$ = CYNew CYPostDecrement($lhs); } ; /* }}} */ /* 12.5 Unary Operators {{{ */ @@ -1156,7 +1241,6 @@ UnaryExpression_ UnaryExpression : PostfixExpression[expression] { $$ = $expression; } - | PostfixExpression[expression] "\n" { $$ = $expression; } | UnaryExpression_[pass] { $$ = $pass; } ; /* }}} */ @@ -1272,11 +1356,6 @@ AssignmentExpression | ArrowFunction[pass] { $$ = $pass; } | LexOf LeftHandSideAssignment[assignment] AssignmentExpression[rhs] { $assignment->SetRight($rhs); $$ = $assignment; } ; - -AssignmentExpressionOpt - : AssignmentExpression[pass] { $$ = $pass; } - | LexOf { $$ = NULL; } - ; /* }}} */ /* 12.15 Comma Operator ( , ) {{{ */ Expression @@ -1346,7 +1425,7 @@ Block ; StatementList - : StatementListItem[statement] StatementListOpt[next] { $statement->SetNext($next); $$ = $statement; } + : StatementListItem[statement] StatementListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } ; StatementListOpt @@ -1525,7 +1604,7 @@ ForStatementInitializer ; ForInStatementInitializer - : LexLet LexOf AccessExpression[pass] LexNewLine { $$ = $pass; } + : LexLet LexOf RubyBlockExpression[pass] { $$ = $pass; } | LexLet LexOf IndirectExpression[pass] { $$ = $pass; } | LexLet LexOf Var_ LexBind ForBinding[binding] { $$ = CYNew CYForVariable($binding); } | ForDeclaration[pass] { $$ = $pass; } @@ -1542,24 +1621,20 @@ 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 {{{ */ -Return - : "return"[return] { if (!driver.return_.top()) CYERR(@return, "invalid 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 {{{ */ @@ -1603,8 +1678,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 {{{ */ @@ -1679,7 +1754,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 @@ -1721,15 +1796,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 {{{ */ @@ -1795,88 +1866,88 @@ ScriptBodyOpt /* }}} */ /* 15.2 Modules {{{ */ Module - : ModuleBodyOpt + : ModuleBodyOpt[code] { driver.script_ = CYNew CYScript($code); } ; ModuleBody - : ModuleItemList + : ModuleItemList[pass] { $$ = $pass; } ; ModuleBodyOpt - : ModuleBody - | + : ModuleBody[pass] { $$ = $pass; } + | LexSetStatement LexLet LexOf { $$ = NULL; } ; ModuleItemList - : ModuleItemListOpt ModuleItem + : ModuleItem[statement] ModuleItemListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } ; ModuleItemListOpt - : ModuleItemList - | + : ModuleItemList[pass] { $$ = $pass; } + | LexSetStatement LexLet LexOf { $$ = NULL; } ; ModuleItem - : LexSetStatement LexLet LexOf ImportDeclaration - | LexSetStatement LexLet LexOf ExportDeclaration - | StatementListItem + : LexSetStatement LexLet LexOf ImportDeclaration[pass] { $$ = $pass; } + | LexSetStatement LexLet LexOf ExportDeclaration { CYNOT(@$); } + | StatementListItem[pass] { $$ = $pass; } ; /* }}} */ /* 15.2.2 Imports {{{ */ ImportDeclaration - : "import" ImportClause FromClause Terminator - | "import" LexOf ModuleSpecifier Terminator + : "import" ImportClause[specifiers] FromClause[module] Terminator { $$ = CYNew CYImportDeclaration($specifiers, $module); } + | "import" LexOf ModuleSpecifier[module] Terminator { $$ = CYNew CYImportDeclaration(NULL, $module); } ; ImportClause - : ImportedDefaultBinding - | LexOf NameSpaceImport - | LexOf NamedImports - | ImportedDefaultBinding "," NameSpaceImport - | ImportedDefaultBinding "," NamedImports + : ImportedDefaultBinding[default] { $$ = $default; } + | LexOf NameSpaceImport[pass] { $$ = $pass; } + | LexOf NamedImports[pass] { $$ = $pass; } + | ImportedDefaultBinding[default] "," NameSpaceImport[next] { $$ = $default; CYSetLast($$) = $next; } + | ImportedDefaultBinding[default] "," NamedImports[next] { $$ = $default; CYSetLast($$) = $next; } ; ImportedDefaultBinding - : ImportedBinding + : ImportedBinding[binding] { $$ = CYNew CYImportSpecifier(CYNew CYIdentifier("default"), $binding); } ; NameSpaceImport - : "*" "as" ImportedBinding + : "*" "as" ImportedBinding[binding] { $$ = CYNew CYImportSpecifier(NULL, $binding); } ; NamedImports - : "{" ImportsListOpt "}" + : "{" ImportsListOpt[pass] "}" { $$ = $pass; } ; FromClause - : "from" ModuleSpecifier + : "from" ModuleSpecifier[pass] { $$ = $pass; } ; ImportsList_ - : "," ImportsListOpt - | + : "," ImportsListOpt[pass] { $$ = $pass; } + | { $$ = NULL; } ; ImportsList - : ImportSpecifier ImportsList_ + : ImportSpecifier[import] ImportsList_[next] { $$ = $import; CYSetLast($$) = $next; } ; ImportsListOpt - : ImportsList - | LexOf + : ImportsList[pass] { $$ = $pass; } + | LexOf { $$ = NULL; } ; ImportSpecifier - : ImportedBinding - | LexOf IdentifierName "as" ImportedBinding + : ImportedBinding[binding] { $$ = CYNew CYImportSpecifier($binding, $binding); } + | LexOf IdentifierName[name] "as" ImportedBinding[binding] { $$ = CYNew CYImportSpecifier($name, $binding); } ; ModuleSpecifier - : StringLiteral + : StringLiteral[pass] { $$ = $pass; } ; ImportedBinding - : BindingIdentifier + : BindingIdentifier[pass] { $$ = $pass; } ; /* }}} */ /* 15.2.3 Exports {{{ */ @@ -1923,63 +1994,138 @@ ExportSpecifier /* Cycript (C): Type Encoding {{{ */ TypeSignifier : IdentifierType[identifier] { $$ = CYNew CYTypedIdentifier(@identifier, $identifier); } - | "(" "*" TypeQualifierRight[typed] ")" { $$ = $typed; } + | "(" "*" TypeQualifierRightOpt[typed] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } + ; + +TypeSignifierNone + : { $$ = CYNew CYTypedIdentifier(@$); } + ; + +TypeSignifierOpt + : TypeSignifier[pass] { $$ = $pass; } + | TypeSignifierNone[pass] { $$ = $pass; } + ; + +Restrict + : "__restrict" + | "restrict" + ; + +RestrictOpt + : Restrict + | + ; + +ParameterModifier + : "throw" "(" ")" + ; + +ParameterModifierOpt + : ParameterModifier + | + ; + +ParameterTail + : TypedParameterListOpt[parameters] ")" ParameterModifierOpt { $$ = CYNew CYTypeFunctionWith($parameters); } ; SuffixedType - : SuffixedType[typed] "[" NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); } - | "(" "^" TypeQualifierRight[typed] ")" "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeBlockWith($parameters, $$->modifier_); } - | TypeSignifier[typed] "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeFunctionWith($parameters, $$->modifier_); } - | "("[parenthesis] TypedParameterListOpt[parameters] ")" { $$ = CYNew CYTypedIdentifier(@parenthesis); $$->modifier_ = CYNew CYTypeFunctionWith($parameters, $$->modifier_); } - | TypeSignifier[pass] { $$ = $pass; } - | { $$ = CYNew CYTypedIdentifier(@$); } + : SuffixedTypeOpt[typed] "[" RestrictOpt NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); } + | "(" "^" TypeQualifierRightOpt[typed] ")" "(" TypedParameterListOpt[parameters] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypeBlockWith($parameters, $$->modifier_); } + | TypeSignifier[typed] "(" ParameterTail[modifier] { $$ = $typed; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + | "("[parenthesis] ParameterTail[modifier] { $$ = CYNew CYTypedIdentifier(@parenthesis); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + ; + +SuffixedTypeOpt + : SuffixedType[pass] { $$ = $pass; } + | TypeSignifierOpt[pass] { $$ = $pass; } ; PrefixedType - : "*" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } + : "*" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } ; TypeQualifierLeft - : { $$ = NULL; } - | "const" TypeQualifierLeft[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeConstant(); } - | "volatile" TypeQualifierLeft[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeVolatile(); } + : "const" TypeQualifierLeftOpt[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeConstant(); } + | "volatile" TypeQualifierLeftOpt[modifier] { $$ = $modifier; CYSetLast($$) = CYNew CYTypeVolatile(); } + ; + +TypeQualifierLeftOpt + : TypeQualifierLeft[pass] { $$ = $pass; } + | { $$ = NULL; } ; TypeQualifierRight - : PrefixedType[pass] { $$ = $pass; } - | SuffixedType[pass] { $$ = $pass; } - | "const" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); } - | "volatile" TypeQualifierRight[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); } + : SuffixedType[pass] { $$ = $pass; } + | PrefixedType[pass] { $$ = $pass; } + | "const" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); } + | "volatile" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); } + | Restrict TypeQualifierRightOpt[typed] { $$ = $typed; } + ; + +TypeQualifierRightOpt + : TypeQualifierRight[pass] { $$ = $pass; } + | TypeSignifierOpt[pass] { $$ = $pass; } ; IntegerType - : "int" { $$ = CYNew CYTypeVariable("int"); } - | "unsigned" IntegerTypeOpt[specifier] { $$ = CYNew CYTypeUnsigned($specifier); } - | "signed" IntegerTypeOpt[specifier] { $$ = CYNew CYTypeSigned($specifier); } - | "long" IntegerTypeOpt[specifier] { $$ = CYNew CYTypeLong($specifier); } - | "short" IntegerTypeOpt[specifier] { $$ = CYNew CYTypeShort($specifier); } + : "int" { $$ = CYNew CYTypeIntegral(CYTypeNeutral); } + | "unsigned" IntegerTypeOpt[integral] { $$ = $integral->Unsigned(); if ($$ == NULL) CYERR(@1, "incompatible unsigned"); } + | "signed" IntegerTypeOpt[integral] { $$ = $integral->Signed(); if ($$ == NULL) CYERR(@1, "incompatible signed"); } + | "long" IntegerTypeOpt[integral] { $$ = $integral->Long(); if ($$ == NULL) CYERR(@1, "incompatible long"); } + | "short" IntegerTypeOpt[integral] { $$ = $integral->Short(); if ($$ == NULL) CYERR(@1, "incompatible short"); } ; IntegerTypeOpt : IntegerType[pass] { $$ = $pass; } - | { $$ = CYNew CYTypeVariable("int"); } + | { $$ = CYNew CYTypeIntegral(CYTypeNeutral); } + ; + +StructFieldListOpt + : TypedIdentifierField[typed] ";" StructFieldListOpt[next] { $$ = CYNew CYTypeStructField($typed, $next); } + | { $$ = NULL; } ; PrimitiveType : IdentifierType[name] { $$ = CYNew CYTypeVariable($name); } | IntegerType[pass] { $$ = $pass; } - | "void" { $$ = CYNew CYTypeVoid(); } - | "char" { $$ = CYNew CYTypeVariable("char"); } - | "signed" "char" { $$ = CYNew CYTypeSigned(CYNew CYTypeVariable("char")); } - | "unsigned" "char" { $$ = CYNew CYTypeUnsigned(CYNew CYTypeVariable("char")); } + | "char" { $$ = CYNew CYTypeCharacter(CYTypeNeutral); } + | "signed" "char" { $$ = CYNew CYTypeCharacter(CYTypeSigned); } + | "unsigned" "char" { $$ = CYNew CYTypeCharacter(CYTypeUnsigned); } + | "struct" IdentifierType[name] { $$ = CYNew CYTypeReference($name); } + ; + +TypedIdentifierMaybe + : TypeQualifierLeft[modifier] "void" TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = CYNew CYTypeVoid(); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + | "void" TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = CYNew CYTypeVoid(); } + | TypeQualifierLeftOpt[modifier] PrimitiveType[specifier] TypeQualifierRightOpt[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; } + ; + +TypedIdentifierField + : TypedIdentifierYes[pass] { $$ = $pass; } + | TypeQualifierLeftOpt[modifier] "struct" "{" StructFieldListOpt[fields] "}" TypeQualifierRightOpt[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); $$ = $typed; $$->specifier_ = CYNew CYTypeStruct(NULL, CYNew CYStructTail($fields)); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + ; + +TypedIdentifierEncoding + : TypedIdentifierNo[pass] { $$ = $pass; } + | TypeQualifierLeftOpt[modifier] "struct" "{" StructFieldListOpt[fields] "}" TypeQualifierRightOpt[typed] { if ($typed->identifier_ != NULL) CYERR($typed->location_, "unexpected identifier"); $$ = $typed; $$->specifier_ = CYNew CYTypeStruct(NULL, CYNew CYStructTail($fields)); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } ; -TypedIdentifier - : TypeQualifierLeft[modifier] PrimitiveType[specifier] TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = $specifier; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } +TypedIdentifierDefinition + : TypedIdentifierYes[pass] { $$ = $pass; } + | TypeQualifierLeftOpt[modifier] "struct" IdentifierTypeOpt[name] "{" StructFieldListOpt[fields] "}" TypeQualifierRightOpt[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); $$ = $typed; $$->specifier_ = CYNew CYTypeStruct($name, CYNew CYStructTail($fields)); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } ; PrimaryExpression - : "@encode" "(" TypedIdentifier[typed] ")" { $$ = CYNew CYEncodedType($typed); } + : "@encode" "(" TypedIdentifierEncoding[typed] ")" { $$ = CYNew CYEncodedType($typed); } ; /* }}} */ @end @@ -1993,21 +2139,17 @@ ClassSuperOpt ; ImplementationFieldListOpt - : TypedIdentifier[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } + : TypedIdentifierField[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } | { $$ = NULL; } ; -ImplementationFields - : "{" ImplementationFieldListOpt[fields] "}" { $$ = $fields; } - ; - MessageScope : "+" { $$ = false; } | "-" { $$ = true; } ; TypeOpt - : "(" TypedIdentifier[type] ")" { if ($type->identifier_ != NULL) CYERR($type->location_, "unexpected identifier"); $$ = $type; } + : "(" TypedIdentifierNo[type] ")" { $$ = $type; } | { $$ = CYNew CYTypedIdentifier(CYNew CYTypeVariable("id")); } ; @@ -2054,7 +2196,7 @@ ClassProtocolListOpt ; ImplementationStatement - : "@implementation" Identifier[name] ClassSuperOpt[extends] ClassProtocolListOpt[protocols] ImplementationFields[fields] ClassMessageDeclarationListOpt[messages] "@end" { $$ = CYNew CYImplementation($name, $extends, $protocols, $fields, $messages); } + : "@implementation" Identifier[name] ClassSuperOpt[extends] ClassProtocolListOpt[protocols] "{" ImplementationFieldListOpt[fields] "}" ClassMessageDeclarationListOpt[messages] "@end" { $$ = CYNew CYImplementation($name, $extends, $protocols, $fields, $messages); } ; CategoryName @@ -2139,15 +2281,29 @@ BoxableExpression | BooleanLiteral[pass] { $$ = $pass; } | NumericLiteral[pass] { $$ = $pass; } | StringLiteral[pass] { $$ = $pass; } - | ArrayLiteral[pass] { $$ = $pass; } - | ObjectLiteral[pass] { $$ = $pass; } | CoverParenthesizedExpressionAndArrowParameterList[pass] { $$ = $pass; } | "YES" { $$ = CYNew CYTrue(); } | "NO" { $$ = CYNew CYFalse(); } ; +KeyValuePairList_ + : "," KeyValuePairListOpt[next] { $$ = $next; } + | { $$ = NULL; } + +KeyValuePairList + : AssignmentExpression[key] ":" AssignmentExpression[value] KeyValuePairList_[next] { $$ = CYNew CYObjCKeyValue($key, $value, $next); } + ; + +KeyValuePairListOpt + : KeyValuePairList[pass] { $$ = $pass; } + | LexOf { $$ = NULL; } + ; + PrimaryExpression : "@" BoxableExpression[expression] { $$ = CYNew CYBox($expression); } + | "@" "[" ElementListOpt[elements] "]" { $$ = CYNew CYObjCArray($elements); } + | "@" "{" KeyValuePairListOpt[pairs] "}" { $$ = CYNew CYObjCDictionary($pairs); } + | "@YES" { $$ = CYNew CYBox(CYNew CYTrue()); } | "@NO" { $$ = CYNew CYBox(CYNew CYFalse()); } | "@true" { $$ = CYNew CYBox(CYNew CYTrue()); } @@ -2157,7 +2313,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 {{{ */ @@ -2194,29 +2350,73 @@ TypedParameterList_ ; TypedParameterList - : TypedIdentifier[typed] TypedParameterList_[next] { $$ = CYNew CYTypedParameter($typed, $next); } + : TypedIdentifierMaybe[typed] TypedParameterList_[next] { $$ = CYNew CYTypedParameter($typed, $next); } ; TypedParameterListOpt : TypedParameterList[pass] { $$ = $pass; } + | "void" { $$ = NULL; } | { $$ = NULL; } ; PrimaryExpression - : "[" LexOf "&" "]" "(" TypedParameterListOpt[parameters] ")" "->" TypedIdentifier[type] "{" FunctionBody[code] "}" { $$ = CYNew CYLambda($type, $parameters, $code); } + : "[" LexOf "&" "]" "(" TypedParameterListOpt[parameters] ")" "->" TypedIdentifierNo[type] "{" FunctionBody[code] "}" { $$ = CYNew CYLambda($type, $parameters, $code); } + ; +/* }}} */ +/* Cycript (C): Structure Definitions {{{ */ +IdentifierNoOf + : "struct" NewLineOpt { $$ = CYNew CYIdentifier("struct"); } + ; + +Statement__ + : "struct" NewLineNot IdentifierType[name] "{" StructFieldListOpt[fields] "}" { $$ = CYNew CYStructDefinition($name, CYNew CYStructTail($fields)); } + ; + +PrimaryExpression + : "(" LexOf "struct" NewLineOpt IdentifierType[name] TypeQualifierRightOpt[typed] ")" { $typed->specifier_ = CYNew CYTypeReference($name); $$ = CYNew CYTypeExpression($typed); } ; /* }}} */ /* Cycript (C): Type Definitions {{{ */ +IdentifierNoOf + : "typedef" NewLineOpt { $$ = CYNew CYIdentifier("typedef"); } + ; + +TypeDefinition + : "typedef" NewLineNot TypedIdentifierDefinition[typed] TerminatorHard { $$ = CYNew CYTypeDefinition($typed); } + ; + Statement__ - : "typedef" TypedIdentifier[typed] { if ($typed->identifier_ == NULL) CYERR($typed->location_, "expected identifier"); } Terminator { $$ = CYNew CYTypeDefinition($typed); } + : TypeDefinition[pass] { $$ = $pass; } + ; + +PrimaryExpression + : "(" LexOf "typedef" NewLineOpt TypedIdentifierEncoding[typed] ")" { $$ = CYNew CYTypeExpression($typed); } ; /* }}} */ /* Cycript (C): extern "C" {{{ */ +IdentifierNoOf + : "extern" NewLineOpt { $$ = CYNew CYIdentifier("extern"); } + ; + +ExternCStatement + : TypedIdentifierField[typed] TerminatorHard { $$ = CYNew CYExternal(CYNew CYString("C"), $typed); } + | TypeDefinition[pass] { $$ = $pass; } + ; + +ExternCStatementListOpt + : ExternCStatement[statement] ExternCStatementListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } + | { $$ = NULL; } + ; + +ExternC + : "{" ExternCStatementListOpt[pass] "}" { $$ = $pass; } + | ExternCStatement[pass] { $$ = $pass; } + ; + 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"); } ExternC[pass] { $$ = $pass; } ; /* }}} */ - @end @begin E4X @@ -2475,8 +2675,14 @@ PrimaryExpression : "{" RubyProcParameters[parameters] StatementListOpt[code] "}" { $$ = CYNew CYRubyProc($parameters, $code); } ; -PostfixExpression - : PostfixExpression[lhs] RubyProcExpression[rhs] LexNewLine { $$ = CYNew CYRubyBlock($lhs, $rhs); } +RubyBlockExpression_ + : AccessExpression[pass] LexNewLineOrOpt { $$ = $pass; } + | RubyBlockExpression_[lhs] RubyProcExpression[rhs] LexNewLineOrOpt { $$ = CYNew CYRubyBlock($lhs, $rhs); } + ; + +RubyBlockExpression + : RubyBlockExpression_[pass] "\n" { $$ = $pass; } + | RubyBlockExpression_[pass] { $$ = $pass; } ; /* }}} */