diff --git a/d.y b/d.y index 8dcc900..2bc9066 100644 --- a/d.y +++ b/d.y @@ -9,7 +9,7 @@ %token colon ":" %token comma "," %token decrement "--" -%token div "/" +%token div_ "/" %token divEqual "/=" %token dollar "$" %token dot "." @@ -173,7 +173,6 @@ %token compilerVersion "__VERSION__" %token file "__FILE__" %token line "__LINE__" -%token comment %token identifier %token scriptLine %token traits @@ -196,37 +195,14 @@ %token stringLiteral %token wstringLiteral + +%{ +#include +%} + %% -Module: ModuleDeclaration DeclDefs - | DeclDefs - -DeclDefs: DeclDef - | DeclDef DeclDefs - -DeclDef: - AttributeSpecifier - | ImportDeclaration - | EnumDeclaration - | ClassDeclaration - | InterfaceDeclaration - | AggregateDeclaration - | Declaration - | Constructor - | Destructor - | UnitTest - | StaticConstructor - | StaticDestructor - | SharedStaticConstructor - | SharedStaticDestructor - | ConditionalDeclaration - | DebugSpecification - | VersionSpecification - | StaticAssert - | TemplateDeclaration - | TemplateMixinDeclaration - | TemplateMixin - | MixinDeclaration - | ";" +Module: ModuleDeclaration Declarations + | Declarations ; ModuleDeclaration: "module" ModuleFullyQualifiedName @@ -256,11 +232,10 @@ ImportList: Import ; Import: ModuleFullyQualifiedName - | ModuleAliasIdentifier "=" ModuleFullyQualifiedName + | identifier "=" ModuleFullyQualifiedName ; -ImportBindings: - Import : ImportBindList +ImportBindings: Import ":" ImportBindList ; ImportBindList: ImportBind @@ -268,65 +243,79 @@ ImportBindList: ImportBind ; ImportBind: identifier - | identifier "=" identifier; - -ModuleAliasIdentifier: - identifier + | identifier "=" identifier ; -MixinDeclaration: "mixin" "(" AssignExpression ")" ";" +Declarations: Declaration + | Declaration Declarations ; -Declaration: AliasDeclaration - | AliasThisDeclaration - | Decl +Declaration: ImportDeclaration + | FunctionDeclaration + | VariableDeclaration + | ";" ; -AliasDeclaration: "alias" BasicType Declarator - | "alias" AliasInitializerList +Statements: Statement + | Statement Statements ; -AliasInitializerList: AliasInitializer - | AliasInitializer "," AliasInitializerList +Statement: IfStatement + | WhileStatement + | DoStatement + | BlockStatement + | ScopeStatement ; -AliasInitializer: identifier "=" Type +IfStatement: "if" "(" IfCondition ")" ScopeStatement + | "if" "(" IfCondition ")" ScopeStatement "else" ScopeStatement ; -AliasThisDeclaration: "alias" identifier "this" +IfCondition: Expression + | "auto" identifier "=" Expression + | BasicType Declarator "=" Expression ; -Decl: StorageClasses Decl - | BasicType Declarators ";" - | BasicType Declarator FunctionBody - | AutoDeclaration +WhileStatement: "while" "(" Expression ")" ScopeStatement ; -Declarators: DeclaratorInitializer - | DeclaratorInitializer "," DeclaratorIdentifierList +DoStatement: "do" ScopeStatement "while" "(" Expression ")" ";" ; -DeclaratorInitializer: Declarator - | Declarator "=" Initializer +ScopeStatement: BlockStatement ; -DeclaratorIdentifierList: DeclaratorIdentifier - | DeclaratorIdentifier "," DeclaratorIdentifierList +BlockStatement: "{" "}" ; -DeclaratorIdentifier: identifier - | identifier "=" Initializer +FunctionDeclaration: Type Name ParameterList FunctionBody + | Type Name ParameterList ";" ; -BasicType: BasicTypeX - | "." IdentifierList - | IdentifierList - | Typeof - | Typeof "." IdentifierList - | "const" "(" Type ")" - | "immutable" "(" Type ")" - | "shared" "(" Type ")" - | "inout" "(" Type ")" +Name: identifier + ; + +Type: identifier + | identifier TypeSuffix + | BasicTypeX + | BasicTypeX TypeSuffix + ; + +TypeSuffix: "*" + | "[" "]" + | "[" Type "]" + ; + +ParameterList: "(" ")" + | "(" Parameters ")" + ; + +Parameters: Type identifier + | Type identifier "," Parameters + ; + +FunctionBody: "{" "}" + | "{" Declarations "}" ; BasicTypeX: "bool" @@ -353,6 +342,38 @@ BasicTypeX: "bool" | "void" ; +LinkageAttribute: "extern" "(" identifier ")" + ; + +AliasDeclaration: "alias" AliasInitializerList + /* | "alias" BasicType Declarator */ + ; + +AliasInitializerList: AliasInitializer + | AliasInitializer "," AliasInitializerList + ; + +AliasInitializer: identifier "=" Type + ; + +VariableDeclaration: Type Names ";" + ; + +Names: identifier + | identifier "," Names + ; + +BasicType: BasicTypeX + | "." IdentifierList + | IdentifierList + | Typeof + | Typeof "." IdentifierList + | "const" "(" Type ")" + | "immutable" "(" Type ")" + | "shared" "(" Type ")" + | "inout" "(" Type ")" + ; + BasicType2: "*" | "[" "]" | "[" AssignExpression "]" @@ -364,6 +385,51 @@ BasicType2: "*" | "function" Parameters FunctionAttributes ; +/*MixinDeclaration: "mixin" "(" AssignExpression ")" ";" + ;*/ + +CastQual: "const" + | "const" "shared" + | "shared" "const" + | "inout" + | "inout" "shared" + | "shared" "inout" + | "immutable" + | "shared" + ; + +/* + +Declaration: AliasDeclaration + | AliasThisDeclaration + | Decl + ; + +AliasThisDeclaration: "alias" identifier "this" + ; + +Decl: StorageClasses Decl + | BasicType Declarators ";" + | BasicType Declarator FunctionBody + | AutoDeclaration + ; + +Declarators: DeclaratorInitializer + | DeclaratorInitializer "," DeclaratorIdentifierList + ; + +DeclaratorInitializer: Declarator + | Declarator "=" Initializer + ; + +DeclaratorIdentifierList: DeclaratorIdentifier + | DeclaratorIdentifier "," DeclaratorIdentifierList + ; + +DeclaratorIdentifier: identifier + | identifier "=" Initializer + ; + Declarator: "(" Declarator ")" | BasicType2 "(" Declarator ")" DeclaratorSuffixes | "(" Declarator ")" DeclaratorSuffixes @@ -417,9 +483,6 @@ StorageClass: "abstract" | "synchronized" ; -Property: "@" PropertyIdentifier - ; - PropertyIdentifier: "property" | "safe" | "trusted" @@ -593,9 +656,6 @@ DeclarationBlock: DeclDef | "{" DeclDefs "}" ; -LinkageAttribute: "extern" "(" LinkageType ")" - ; - LinkageType: "C" | "C++" | "D" @@ -619,7 +679,7 @@ StorageClass: UserDefinedAttribute ; UserDefinedAttribute: "@" "(" "ArgumentList" ")" - | "@" identifier /* BUG: Should be CallExpression but CallExpression is not defined by the grammar */ + | "@" identifier ; Pragma: "pragma" "(" identifier ")" @@ -776,16 +836,6 @@ CastExpression: "cast" "(" Type ")" UnaryExpression | "cast" "(" ")" UnaryExpression ; -CastQual: "const" - | "const" "shared" - | "shared" "const" - | "inout" - | "inout" "shared" - | "shared" "inout" - | "immutable" - | "shared" - ; - PowExpression: PostfixExpression | PostfixExpression "^^" UnaryExpression ; @@ -831,9 +881,7 @@ PrimaryExpression: identifier | intLiteral | floatLiteral | characterLiteral - | stringLiteral - | wstringLiteral - | dstringLiteral + | StringLiterals | ArrayLiteral | AssocArrayLiteral | Lambda @@ -1168,9 +1216,9 @@ ScopeGuardStatement: "scope" "(" "exit" ")" NonEmptyOrScopeBlockStatement ; AsmStatement: "asm" "{" "}" -/* asm { AsmInstructionList } +asm { AsmInstructionList } AsmInstructionList: AsmInstruction ";" - AsmInstruction ";" AsmInstructionList*/ + AsmInstruction ";" AsmInstructionList PragmaStatement: Pragma NoScopeStatement ; @@ -1303,7 +1351,7 @@ InterfaceDeclaration: "interface" identifier InterfaceBody | InterfaceTemplateDeclaration ; -BaseInterfaceList: ":" Interfaces /* BUG: Spec uses InterfaceClasses here */ +BaseInterfaceList: ":" Interfaces ; InterfaceBody: "{" "}" @@ -1587,41 +1635,6 @@ StaticAssert: "static" "assert" "(" AssignExpression ")" ";" TraitsExpression: "__traits" "(" TraitsKeyword "," TraitsArguments ")" -TraitsKeyword: "isAbstractClass" - | "isArithmetic" - | "isAssociativeArray" - | "isFinalClass" - | "isPOD" - | "isNested" - | "isFloating" - | "isIntegral" - | "isScalar" - | "isStaticArray" - | "isUnsigned" - | "isVirtualFunction" - | "isVirtualMethod" - | "isAbstractFunction" - | "isFinalFunction" - | "isStaticFunction" - | "isRef" - | "isOut" - | "isLazy" - | "hasMember" - | "identifier" - | "getAttributes" - | "getMember" - | "getOverloads" - | "getProtection" - | "getVirtualFunctions" - | "getVirtualMethods" - | "parent" - | "classInstanceSize" - | "allMembers" - | "derivedMembers" - | "isSame" - | "compiles" - ; - TraitsArguments: TraitsArgument TraitsArgument "," TraitsArguments ; @@ -1630,13 +1643,9 @@ TraitsArgument: AssignExpression | Type ; -SpecialKeywords: "__FILE__" - | "__MODULE__" - | "__LINE__" - | "__FUNCTION__" - | " __PRETTY_FUNCTION__" - ; - UnitTest: "unittest" FunctionBody ; + +*/ %% + diff --git a/dlex.txt b/dlex.txt new file mode 100644 index 0000000..d515d11 --- /dev/null +++ b/dlex.txt @@ -0,0 +1,286 @@ +%option noyywrap +%{ +#include "d.tab.h" +%} + + +ident [_[:alpha:]]* +whitespace [\n\t\r ] + +assign "=" +at "@" +bitAnd "&" +bitAndEqual "&=" +bitOr "\|" +bitOrEqual "\|=" +catEqual "~=" +colon ":" +comma "," +decrement "--" +div_ "\/" +divEqual "\/=" +dollar "\$" +dot "\." +equal "==" +goesTo "=>" +greater ">" +greaterEqual ">=" +hash "#" +increment "++" +lBrace "{" +lBracket "[" +less "<" +lessEqual "<=" +lessEqualGreater "<>=" +lessOrGreater "<>" +logicAnd "&&" +logicOr "||" +lParen "(" +minus "-" +minusEqual "-=" +mod "%" +modEqual "%=" +mulEqual "*=" +not "!" +notEqual "!=" +notGreater "!>" +notGreaterEqual "!>=" +notLess "!<" +notLessEqual "!<=" +notLessEqualGreater "!<>" +plus "+" +plusEqual "+=" +pow "^^" +powEqual "^^=" +rBrace "}" +rBracket "]" +rParen ")" +semicolon ";" +shiftLeft "<<" +shiftLeftEqual "<<=" +shiftRight ">>" +shiftRightEqual ">>=" +slice ".." +star "*" +ternary "?" +tilde "~" +unordered "!<>=" +unsignedShiftRight ">>>" +unsignedShiftRightEqual ">>>=" +vararg "..." +xor "^" +xorEqual "^=" +bool bool +byte byte +cdouble cdouble +cent cent +cfloat cfloat +char char +creal creal +dchar dchar +double double +float float +function function +idouble idouble +ifloat ifloat +int int +ireal ireal +long long +real real +short short +ubyte ubyte +ucent ucent +uint uint +ulong ulong +ushort ushort +void void +wchar wchar +align align +deprecated deprecated +extern extern +pragma pragma +export export +package package +private private +protected protected +public public +abstract abstract +auto auto +const const +final final +gshared __gshared +immutable immutable +inout inout +scope scope +shared shared +static static +synchronized synchronized +alias alias +asm asm +assert assert +body body +break break +case case +cast cast +catch catch +class class +continue continue +debug debug +default default +delegate delegate +delete delete +do do +else else +enum enum +false false +finally finally +foreach foreach +foreach_reverse foreach_reverse +for for +goto goto +if if +import import +in in +interface interface +invariant invariant +is is +lazy lazy +macro macro +mixin mixin +module module +new new +nothrow nothrow +null null +out out +override override +pure pure +ref ref +return return +struct struct +super super +switch switch +template template +this this +throw throw +true true +try try +typedef typedef +typeid typeid +typeof typeof +union union +unittest unittest +version version +volatile volatile +while while +with with +date __DATE__ +eof __EOF__ +time __TIME__ +timestamp __TIMESTAMP__ +vendor __VENDOR__ +compilerVersion __VERSION__ +file __FILE__ +line __LINE__ + +%% +{import} return import_; +{static} return static_; +{bool} return bool_; +{byte} return byte_; +{ubyte} return ubyte_; +{short} return short_; +{ushort} return ushort_; +{int} return int_; +{uint} return uint_; +{long} return long_; +{ulong} return ulong_; +{char} return char_; +{wchar} return wchar_; +{dchar} return dchar_; +{float} return float_; +{double} return double_; +{real} return real_; +{ifloat} return ifloat_; +{idouble} return idouble_; +{ireal} return ireal_; +{cfloat} return cfloat_; +{cdouble} return cdouble_; +{creal} return creal_; +{void} return void_; +{ident} return identifier; +{whitespace} ; +{assign} return assign; +{at} return at; +{bitAnd} return bitAnd; +{bitAndEqual} return bitAndEqual; +{bitOr} return bitOr; +{bitOrEqual} return bitOrEqual; +{catEqual} return catEqual; +{colon} return colon; +{comma} return comma; +{decrement} return decrement; +{div_} return div_; +{divEqual} return divEqual; +{dollar} return dollar; +{dot} return dot; +{equal} return equal; +{goesTo} return goesTo; +{greater} return greater; +{greaterEqual} return greaterEqual; +{hash} return hash; +{increment} return increment; +{lBrace} return lBrace; +{lBracket} return lBracket; +{less} return less; +{lessEqual} return lessEqual; +{lessEqualGreater} return lessEqualGreater; +{lessOrGreater} return lessOrGreater; +{logicAnd} return logicAnd; +{logicOr} return logicOr; +{lParen} return lParen; +{minus} return minus; +{minusEqual} return minusEqual; +{mod} return mod; +{modEqual} return modEqual; +{mulEqual} return mulEqual; +{not} return not; +{notEqual} return notEqual; +{notGreater} return notGreater; +{notGreaterEqual} return notGreaterEqual; +{notLess} return notLess; +{notLessEqual} return notLessEqual; +{notLessEqualGreater} return notLessEqualGreater; +{plus} return plus; +{plusEqual} return plusEqual; +{pow} return pow; +{powEqual} return powEqual; +{rBrace} return rBrace; +{rBracket} return rBracket; +{rParen} return rParen; +{semicolon} return semicolon; +{shiftLeft} return shiftLeft; +{shiftLeftEqual} return shiftLeftEqual; +{shiftRight} return shiftRight; +{shiftRightEqual} return shiftRightEqual; +{slice} return slice; +{star} return star; +{ternary} return ternary; +{tilde} return tilde; +{unordered} return unordered; +{unsignedShiftRight} return unsignedShiftRight; +{unsignedShiftRightEqual} return unsignedShiftRightEqual; +{vararg} return vararg; +{xor} return xor; +{xorEqual} return xorEqual; +%% + +int main() +{ + yyparse(); + return 0; +} + +int yyerror(char* err) +{ + fprintf(stderr, "%s\n", err); +}