X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/8e216accc5c795e2cf42f89276841b175ed33f58..88085bb43c2b0053fbb8d6b62b00af71c4b323cf:/Parser.ypp.in diff --git a/Parser.ypp.in b/Parser.ypp.in index 88c01bf..ac42373 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_; } @@ -357,6 +358,7 @@ type; }) %token _null_ "null" %token _true_ "true" +%token _as_ "as" %token _break_ "break" %token _case_ "case" %token _catch_ "catch" @@ -422,6 +424,8 @@ type; }) %token ___proto___ "__proto__" %token _prototype_ "prototype" %token _public_ "public" +%token ___restrict_ "__restrict" +%token _restrict_ "restrict" %token _set_ "set" %token _short_ "short" %token _static_ "static" @@ -524,6 +528,9 @@ type; }) %type ExpressionOpt %type ExpressionStatement_ %type ExpressionStatement +%type ExternC +%type ExternCStatement +%type ExternCStatementListOpt %type Finally %type ForBinding %type ForDeclaration @@ -533,6 +540,7 @@ type; }) %type FormalParameterList_ %type FormalParameterList %type FormalParameters +%type FromClause %type FunctionBody %type FunctionDeclaration %type FunctionExpression @@ -550,6 +558,14 @@ type; }) %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 @@ -570,8 +586,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 @@ -613,6 +637,7 @@ type; }) %type TemplateSpans %type ThrowStatement %type TryStatement +%type TypeDefinition %type UnaryExpression_ %type UnaryExpression %type VariableDeclaration @@ -636,11 +661,16 @@ type; }) %type SuffixedType %type SuffixedTypeOpt %type TypeSignifier +%type TypeSignifierNone %type TypeSignifierOpt +%type ParameterTail %type TypeQualifierLeft %type TypeQualifierLeftOpt %type TypeQualifierRight %type TypeQualifierRightOpt +%type TypedIdentifierDefinition +%type TypedIdentifierEncoding +%type TypedIdentifierField %type TypedIdentifierMaybe %type TypedIdentifierNo %type TypedIdentifierYes @@ -865,6 +895,11 @@ TerminatorSoft | NewLineNot LexOf Terminator ; +TerminatorHard + : ";" + | error { if (yyla.type_get() != yyeof_) CYERR(@error, "required semi-colon"); else CYEOK(); } StrictSemi + ; + Terminator : ";" | error { if (yyla.type_get() != yyeof_ && yyla.type != yytranslate_(token::CloseBrace) && !driver.newline_) CYERR(@error, "required semi-colon"); else CYEOK(); } StrictSemi @@ -901,6 +936,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"); } @@ -954,12 +990,13 @@ IdentifierNoOf | "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 | "signed" { $$ = CYNew CYIdentifier("signed"); } - | "struct" { $$ = CYNew CYIdentifier("struct"); } | "unsigned" { $$ = CYNew CYIdentifier("unsigned"); } @end @begin ObjectiveC @@ -1378,7 +1415,7 @@ Block ; StatementList - : StatementListItem[statement] StatementListOpt[next] { $statement->SetNext($next); $$ = $statement; } + : StatementListItem[statement] StatementListOpt[next] { $$ = $statement; CYSetLast($$) = $next; } ; StatementListOpt @@ -1819,88 +1856,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 {{{ */ @@ -1950,16 +1987,43 @@ TypeSignifier | "(" "*" TypeQualifierRightOpt[typed] ")" { $$ = $typed; $$->modifier_ = CYNew CYTypePointerTo($$->modifier_); } ; +TypeSignifierNone + : { $$ = CYNew CYTypedIdentifier(@$); } + ; + TypeSignifierOpt : TypeSignifier[pass] { $$ = $pass; } - | { $$ = CYNew CYTypedIdentifier(@$); } + | TypeSignifierNone[pass] { $$ = $pass; } + ; + +Restrict + : "__restrict" + | "restrict" + ; + +RestrictOpt + : Restrict + | + ; + +ParameterModifier + : "throw" "(" ")" + ; + +ParameterModifierOpt + : ParameterModifier + | + ; + +ParameterTail + : TypedParameterListOpt[parameters] ")" ParameterModifierOpt { $$ = CYNew CYTypeFunctionWith($parameters); } ; SuffixedType - : SuffixedTypeOpt[typed] "[" NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); } + : SuffixedTypeOpt[typed] "[" RestrictOpt NumericLiteral[size] "]" { $$ = $typed; $$->modifier_ = CYNew CYTypeArrayOf($size, $$->modifier_); } | "(" "^" TypeQualifierRightOpt[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[typed] "(" ParameterTail[modifier] { $$ = $typed; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + | "("[parenthesis] ParameterTail[modifier] { $$ = CYNew CYTypedIdentifier(@parenthesis); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } ; SuffixedTypeOpt @@ -1986,6 +2050,7 @@ TypeQualifierRight | PrefixedType[pass] { $$ = $pass; } | "const" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeConstant($$->modifier_); } | "volatile" TypeQualifierRightOpt[typed] { $$ = $typed; $$->modifier_ = CYNew CYTypeVolatile($$->modifier_); } + | Restrict TypeQualifierRightOpt[typed] { $$ = $typed; } ; TypeQualifierRightOpt @@ -2007,7 +2072,7 @@ IntegerTypeOpt ; StructFieldListOpt - : TypedIdentifierMaybe[typed] ";" StructFieldListOpt[next] { $$ = CYNew CYTypeStructField($typed, $next); } + : TypedIdentifierField[typed] ";" StructFieldListOpt[next] { $$ = CYNew CYTypeStructField($typed, $next); } | { $$ = NULL; } ; @@ -2017,13 +2082,13 @@ PrimitiveType | "char" { $$ = CYNew CYTypeVariable("char"); } | "signed" "char" { $$ = CYNew CYTypeSigned(CYNew CYTypeVariable("char")); } | "unsigned" "char" { $$ = CYNew CYTypeUnsigned(CYNew CYTypeVariable("char")); } - | "struct" IdentifierTypeOpt[name] "{" StructFieldListOpt[fields] "}" { $$ = CYNew CYTypeStruct($name, $fields); } + | "struct" IdentifierType[name] { $$ = CYNew CYTypeReference($name); } ; TypedIdentifierMaybe - : TypeQualifierLeftOpt[modifier] PrimitiveType[specifier] TypeQualifierRightOpt[typed] { $$ = $typed; $$->specifier_ = $specifier; CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } - | TypeQualifierLeft[modifier] "void" TypeQualifierRight[typed] { $$ = $typed; $$->specifier_ = CYNew CYTypeVoid(); CYSetLast($modifier) = $$->modifier_; $$->modifier_ = $modifier; } + : 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 @@ -2034,8 +2099,23 @@ 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; } + ; + +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" "(" TypedIdentifierMaybe[typed] ")" { $$ = CYNew CYEncodedType($typed); } + : "@encode" "(" TypedIdentifierEncoding[typed] ")" { $$ = CYNew CYEncodedType($typed); } ; /* }}} */ @end @@ -2049,7 +2129,7 @@ ClassSuperOpt ; ImplementationFieldListOpt - : TypedIdentifierMaybe[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } + : TypedIdentifierField[typed] ";" ImplementationFieldListOpt[next] { $$ = CYNew CYImplementationField($typed, $next); } | { $$ = NULL; } ; @@ -2256,7 +2336,20 @@ TypedParameterListOpt ; PrimaryExpression - : "[" LexOf "&" "]" "(" TypedParameterListOpt[parameters] ")" "->" TypedIdentifierMaybe[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 {{{ */ @@ -2264,12 +2357,16 @@ IdentifierNoOf : "typedef" NewLineOpt { $$ = CYNew CYIdentifier("typedef"); } ; +TypeDefinition + : "typedef" NewLineNot TypedIdentifierDefinition[typed] TerminatorHard { $$ = CYNew CYTypeDefinition($typed); } + ; + Statement__ - : "typedef" NewLineNot TypedIdentifierYes[typed] Terminator { $$ = CYNew CYTypeDefinition($typed); } + : TypeDefinition[pass] { $$ = $pass; } ; PrimaryExpression - : "(" LexOf "typedef" NewLineOpt TypedIdentifierNo[typed] ")" { $$ = CYNew CYTypeExpression($typed); } + : "(" LexOf "typedef" NewLineOpt TypedIdentifierEncoding[typed] ")" { $$ = CYNew CYTypeExpression($typed); } ; /* }}} */ /* Cycript (C): extern "C" {{{ */ @@ -2277,11 +2374,25 @@ 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" NewLineNot StringLiteral[abi] { if (strcmp($abi->Value(), "C") != 0) CYERR(@abi, "unknown extern binding"); } TypedIdentifierYes[typed] 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