grammar work

This commit is contained in:
Hackerpilot 2013-04-03 12:16:01 -07:00
parent 281751e52e
commit 2c56b01c15
2 changed files with 430 additions and 135 deletions

279
d.y
View File

@ -9,7 +9,7 @@
%token colon ":" %token colon ":"
%token comma "," %token comma ","
%token decrement "--" %token decrement "--"
%token div "/" %token div_ "/"
%token divEqual "/=" %token divEqual "/="
%token dollar "$" %token dollar "$"
%token dot "." %token dot "."
@ -173,7 +173,6 @@
%token compilerVersion "__VERSION__" %token compilerVersion "__VERSION__"
%token file "__FILE__" %token file "__FILE__"
%token line "__LINE__" %token line "__LINE__"
%token comment
%token identifier %token identifier
%token scriptLine %token scriptLine
%token traits %token traits
@ -196,37 +195,14 @@
%token stringLiteral %token stringLiteral
%token wstringLiteral %token wstringLiteral
%{
#include <stdio.h>
%}
%% %%
Module: ModuleDeclaration DeclDefs Module: ModuleDeclaration Declarations
| DeclDefs | Declarations
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
| ";"
; ;
ModuleDeclaration: "module" ModuleFullyQualifiedName ModuleDeclaration: "module" ModuleFullyQualifiedName
@ -256,11 +232,10 @@ ImportList: Import
; ;
Import: ModuleFullyQualifiedName Import: ModuleFullyQualifiedName
| ModuleAliasIdentifier "=" ModuleFullyQualifiedName | identifier "=" ModuleFullyQualifiedName
; ;
ImportBindings: ImportBindings: Import ":" ImportBindList
Import : ImportBindList
; ;
ImportBindList: ImportBind ImportBindList: ImportBind
@ -268,65 +243,79 @@ ImportBindList: ImportBind
; ;
ImportBind: identifier ImportBind: identifier
| identifier "=" identifier; | identifier "=" identifier
ModuleAliasIdentifier:
identifier
; ;
MixinDeclaration: "mixin" "(" AssignExpression ")" ";" Declarations: Declaration
| Declaration Declarations
; ;
Declaration: AliasDeclaration Declaration: ImportDeclaration
| AliasThisDeclaration | FunctionDeclaration
| Decl | VariableDeclaration
| ";"
; ;
AliasDeclaration: "alias" BasicType Declarator Statements: Statement
| "alias" AliasInitializerList | Statement Statements
; ;
AliasInitializerList: AliasInitializer Statement: IfStatement
| AliasInitializer "," AliasInitializerList | 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 WhileStatement: "while" "(" Expression ")" ScopeStatement
| BasicType Declarators ";"
| BasicType Declarator FunctionBody
| AutoDeclaration
; ;
Declarators: DeclaratorInitializer DoStatement: "do" ScopeStatement "while" "(" Expression ")" ";"
| DeclaratorInitializer "," DeclaratorIdentifierList
; ;
DeclaratorInitializer: Declarator ScopeStatement: BlockStatement
| Declarator "=" Initializer
; ;
DeclaratorIdentifierList: DeclaratorIdentifier BlockStatement: "{" "}"
| DeclaratorIdentifier "," DeclaratorIdentifierList
; ;
DeclaratorIdentifier: identifier FunctionDeclaration: Type Name ParameterList FunctionBody
| identifier "=" Initializer | Type Name ParameterList ";"
; ;
BasicType: BasicTypeX Name: identifier
| "." IdentifierList ;
| IdentifierList
| Typeof Type: identifier
| Typeof "." IdentifierList | identifier TypeSuffix
| "const" "(" Type ")" | BasicTypeX
| "immutable" "(" Type ")" | BasicTypeX TypeSuffix
| "shared" "(" Type ")" ;
| "inout" "(" Type ")"
TypeSuffix: "*"
| "[" "]"
| "[" Type "]"
;
ParameterList: "(" ")"
| "(" Parameters ")"
;
Parameters: Type identifier
| Type identifier "," Parameters
;
FunctionBody: "{" "}"
| "{" Declarations "}"
; ;
BasicTypeX: "bool" BasicTypeX: "bool"
@ -353,6 +342,38 @@ BasicTypeX: "bool"
| "void" | "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: "*" BasicType2: "*"
| "[" "]" | "[" "]"
| "[" AssignExpression "]" | "[" AssignExpression "]"
@ -364,6 +385,51 @@ BasicType2: "*"
| "function" Parameters FunctionAttributes | "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 ")" Declarator: "(" Declarator ")"
| BasicType2 "(" Declarator ")" DeclaratorSuffixes | BasicType2 "(" Declarator ")" DeclaratorSuffixes
| "(" Declarator ")" DeclaratorSuffixes | "(" Declarator ")" DeclaratorSuffixes
@ -417,9 +483,6 @@ StorageClass: "abstract"
| "synchronized" | "synchronized"
; ;
Property: "@" PropertyIdentifier
;
PropertyIdentifier: "property" PropertyIdentifier: "property"
| "safe" | "safe"
| "trusted" | "trusted"
@ -593,9 +656,6 @@ DeclarationBlock: DeclDef
| "{" DeclDefs "}" | "{" DeclDefs "}"
; ;
LinkageAttribute: "extern" "(" LinkageType ")"
;
LinkageType: "C" LinkageType: "C"
| "C++" | "C++"
| "D" | "D"
@ -619,7 +679,7 @@ StorageClass: UserDefinedAttribute
; ;
UserDefinedAttribute: "@" "(" "ArgumentList" ")" UserDefinedAttribute: "@" "(" "ArgumentList" ")"
| "@" identifier /* BUG: Should be CallExpression but CallExpression is not defined by the grammar */ | "@" identifier
; ;
Pragma: "pragma" "(" identifier ")" Pragma: "pragma" "(" identifier ")"
@ -776,16 +836,6 @@ CastExpression: "cast" "(" Type ")" UnaryExpression
| "cast" "(" ")" UnaryExpression | "cast" "(" ")" UnaryExpression
; ;
CastQual: "const"
| "const" "shared"
| "shared" "const"
| "inout"
| "inout" "shared"
| "shared" "inout"
| "immutable"
| "shared"
;
PowExpression: PostfixExpression PowExpression: PostfixExpression
| PostfixExpression "^^" UnaryExpression | PostfixExpression "^^" UnaryExpression
; ;
@ -831,9 +881,7 @@ PrimaryExpression: identifier
| intLiteral | intLiteral
| floatLiteral | floatLiteral
| characterLiteral | characterLiteral
| stringLiteral | StringLiterals
| wstringLiteral
| dstringLiteral
| ArrayLiteral | ArrayLiteral
| AssocArrayLiteral | AssocArrayLiteral
| Lambda | Lambda
@ -1168,9 +1216,9 @@ ScopeGuardStatement: "scope" "(" "exit" ")" NonEmptyOrScopeBlockStatement
; ;
AsmStatement: "asm" "{" "}" AsmStatement: "asm" "{" "}"
/* asm { AsmInstructionList } asm { AsmInstructionList }
AsmInstructionList: AsmInstruction ";" AsmInstructionList: AsmInstruction ";"
AsmInstruction ";" AsmInstructionList*/ AsmInstruction ";" AsmInstructionList
PragmaStatement: Pragma NoScopeStatement PragmaStatement: Pragma NoScopeStatement
; ;
@ -1303,7 +1351,7 @@ InterfaceDeclaration: "interface" identifier InterfaceBody
| InterfaceTemplateDeclaration | InterfaceTemplateDeclaration
; ;
BaseInterfaceList: ":" Interfaces /* BUG: Spec uses InterfaceClasses here */ BaseInterfaceList: ":" Interfaces
; ;
InterfaceBody: "{" "}" InterfaceBody: "{" "}"
@ -1587,41 +1635,6 @@ StaticAssert: "static" "assert" "(" AssignExpression ")" ";"
TraitsExpression: "__traits" "(" TraitsKeyword "," TraitsArguments ")" 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 TraitsArguments: TraitsArgument
TraitsArgument "," TraitsArguments TraitsArgument "," TraitsArguments
; ;
@ -1630,13 +1643,9 @@ TraitsArgument: AssignExpression
| Type | Type
; ;
SpecialKeywords: "__FILE__"
| "__MODULE__"
| "__LINE__"
| "__FUNCTION__"
| " __PRETTY_FUNCTION__"
;
UnitTest: "unittest" FunctionBody UnitTest: "unittest" FunctionBody
; ;
*/
%% %%

286
dlex.txt Normal file
View File

@ -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);
}