DGrok Delphi Grammar

DGrok is my project to write a parser for the Delphi language, and then to build interesting tools on top of that parser. For more information on DGrok, see the DGrok posts on my blog.

This page shows the Delphi grammar as I've puzzled it out so far, and indicates how much of it I've written a working parser for. Solid underline means that rule is completely implemented in my parser; broken underline means partly implemented; no underline means something I haven't started yet.

Current status: Completed 98 rules of 98 = 100% complete.

Last updated Wed Oct 03 22:48:00 Central Daylight Time 2007.

Joe White

Table of Contents

AddOpExceptionItemGotoStatementOpenArrayRecordTypeUnaryOperator
ArrayTypeExportsItemIdentPackageRelOpUnit
AssemblerStatementExportsSpecifierIdentListPackedTypeRepeatStatementUsedUnit
AssemblyAttributeExportsStatementIfStatementParameterRequiresClauseUsesClause
AtomExpressionImplementationDeclParameterExpressionSetLiteralVarDecl
BareInheritedExpressionListImplementationSectionParameterTypeSetTypeVariantGroup
BlockExpressionOrAssignmentInitSectionParenthesizedExpressionSimpleExpressionVariantSection
CaseSelectorExpressionOrRangeInterfaceDeclParticleSimpleStatementVarSection
CaseStatementExpressionOrRangeListInterfaceSectionPointerTypeStatementVisibility
ClassHelperTypeExtendedIdentInterfaceTypePortabilityDirectiveStatementListVisibilitySection
ClassOfTypeFactorLabelDeclSectionProcedureTypeStringTypeVisibilitySectionContent
ClassTypeFancyBlockLabelIdProgramTermWhileStatement
ConstantDeclFieldDeclMethodHeadingPropertyTryStatementWithStatement
ConstSectionFieldSectionMethodImplementationPropertyDirectiveType
DirectiveFileTypeMethodOrPropertyQualifiedIdentTypedConstant
EnumeratedTypeForStatementMethodReturnTypeRaiseStatementTypeDecl
EnumeratedTypeElementGoalMulOpRecordHelperTypeTypeSection

Likely Targets (0)

These rules look like their dependencies are met, and are likely targets to be implemented next. Numbers in parentheses show how many places the rule is used.

AddOp [^]

(Completed) -> '+'
(Completed) -> '-'
(Completed) -> OR
(Completed) -> XOR

ArrayType [^]

(Completed) -> ARRAY ['[' (Type [','])+ ']'] OF Type

AssemblerStatement [^]

(Completed) -> ASM
(Completed)    <assemblylanguage>
(Completed)    END

AssemblyAttribute [^]

(Completed) -> '[' ASSEMBLY ':' Expression ']'

Atom [^]

(Completed) -> Particle
(Completed)    ( '.' ExtendedIdent
(Completed)    | '[' ExpressionList ']'
(Completed)    | '^'
(Completed)    | '(' (ParameterExpression [','])* ')'
(Completed)    )*

BareInherited [^]

(Completed) -> INHERITED

Block [^]

(Completed) -> BEGIN [StatementList] END
(Completed) -> AssemblerStatement

CaseSelector [^]

(Completed) -> (ExpressionOrRange [','])+
(Completed)    ':' [Statement] [';']

CaseStatement [^]

(Completed) -> CASE Expression OF
(Completed)    (CaseSelector)+
(Completed)    [ELSE [StatementList]]
(Completed)    END

ClassHelperType [^]

(Completed) -> CLASS HELPER
(Completed)    ['(' QualifiedIdent ')']
(Completed)    FOR QualifiedIdent
(Completed)    (VisibilitySection)*
(Completed)    END

ClassOfType [^]

(Completed) -> CLASS OF QualifiedIdent

ClassType [^]

(Completed) -> CLASS
(Completed)    [ABSTRACT | SEALED]
(Completed)    ['(' (QualifiedIdent [','])+ ')']

The remainder is optional, but only if the base class is specified and lookahead shows that the next token is a semicolon

(Completed)    (VisibilitySection)*
(Completed)    END

ConstantDecl [^]

(Completed) -> Ident
(Completed)    [':' Type]
(Completed)    '=' TypedConstant
(Completed)    (PortabilityDirective)*
(Completed)    ';'

ConstSection [^]

(Completed) -> (CONST|RESOURCESTRING)
(Completed)    (ConstantDecl)+

Directive [^]

(Completed) -> [';'] ABSTRACT
(Completed) -> [';'] ASSEMBLER
(Completed) -> [';'] CDECL
(Completed) -> [';'] DISPID Expression
(Completed) -> [';'] DYNAMIC
(Completed) -> [';'] EXPORT
(Completed) -> [';'] EXTERNAL [Expression (ExportsSpecifier)*]
(Completed) -> [';'] FAR
(Completed) -> [';'] FINAL
(Completed) -> [';'] FORWARD
(Completed) -> [';'] INLINE
(Completed) -> [';'] LOCAL
(Completed) -> [';'] MESSAGE Expression
(Completed) -> [';'] NEAR
(Completed) -> [';'] OVERLOAD
(Completed) -> [';'] OVERRIDE
(Completed) -> [';'] PASCAL
(Completed) -> [';'] REGISTER
(Completed) -> [';'] REINTRODUCE
(Completed) -> [';'] SAFECALL
(Completed) -> [';'] STATIC
(Completed) -> [';'] STDCALL
(Completed) -> [';'] VARARGS
(Completed) -> [';'] VIRTUAL
(Completed) -> [';'] PortabilityDirective

EnumeratedType [^]

(Completed) -> '(' (EnumeratedTypeElement [','])+ ')'

EnumeratedTypeElement [^]

(Completed) -> Ident [ '=' Expression ]

ExceptionItem [^]

(Completed) -> ON
(Completed)    [Ident ':']
(Completed)    QualifiedIdent DO
(Completed)    [Statement]
(Completed)    [';']

ExportsItem [^]

(Completed) -> Ident (ExportsSpecifier)*

ExportsSpecifier [^]

(Completed) -> (INDEX | NAME) Expression

ExportsStatement [^]

(Completed) -> EXPORTS (ExportsItem [','])+ ';'

Expression [^]

(Completed) -> SimpleExpression (RelOp SimpleExpression)*

ExpressionList [^]

(Completed) -> (Expression [','])+

ExpressionOrAssignment [^]

(Completed) -> Expression
(Completed) -> Expression ':=' Expression

ExpressionOrRange [^]

(Completed) -> SimpleExpression ['..' SimpleExpression]

ExpressionOrRangeList [^]

(Completed) -> (ExpressionOrRange [','])+

ExtendedIdent [^]

(Completed) -> Ident
(Completed) -> <keyword>

Factor [^]

(Completed) -> Atom
(Completed) -> UnaryOperator Factor

FancyBlock [^]

(Completed) -> (ImplementationDecl)*
(Completed)    Block

FieldDecl [^]

(Completed) -> IdentList ':' Type (PortabilityDirective)* [';']

FieldSection [^]

(Completed) -> [[CLASS] VAR]
(Completed)    (FieldDecl)*

FileType [^]

(Completed) -> FILE
(Completed) -> FILE OF QualifiedIdent

ForStatement [^]

(Completed) -> FOR Ident ':=' Expression (TO | DOWNTO) Expression DO [Statement]
(Completed) -> FOR Ident IN Expression DO [Statement]

Goal [^]

(Completed) -> Program
(Completed) -> Package
(Completed) -> Unit

GotoStatement [^]

(Completed) -> GOTO LabelId

Ident [^]

(Completed) -> <identifier>
(Completed) -> <semikeyword>
(Completed) -> '&' <identifier>
(Completed) -> '&' <semikeyword>
(Completed) -> '&' <keyword>

IdentList [^]

(Completed) -> (Ident [','])+

IfStatement [^]

(Completed) -> IF Expression THEN [Statement]
(Completed)    [ELSE [Statement]]

ImplementationDecl [^]

(Completed) -> LabelDeclSection
(Completed) -> ConstSection
(Completed) -> TypeSection
(Completed) -> VarSection
(Completed) -> MethodImplementation
(Completed) -> ExportsStatement
(Completed) -> AssemblyAttribute

ImplementationSection [^]

(Completed) -> IMPLEMENTATION
(Completed)    [UsesClause]
(Completed)    (ImplementationDecl)*

InitSection [^]

(Completed) -> END
(Completed) -> Block
(Completed) -> INITIALIZATION
(Completed)    [StatementList]
(Completed)    [FINALIZATION
(Completed)    [StatementList]]
(Completed)    END

InterfaceDecl [^]

(Completed) -> ConstSection
(Completed) -> TypeSection
(Completed) -> VarSection
(Completed) -> MethodHeading

InterfaceSection [^]

(Completed) -> INTERFACE
(Completed)    [UsesClause]
(Completed)    (InterfaceDecl)*

InterfaceType [^]

(Completed) -> (INTERFACE | DISPINTERFACE)
(Completed)    ['(' QualifiedIdent ')']
(Completed)    ['[' Expression ']']
(Completed)    (MethodOrProperty)*
(Completed)    END

LabelDeclSection [^]

(Completed) -> LABEL (LabelId [','])+ ';'

LabelId [^]

(Completed) -> <number>
(Completed) -> Ident

MethodHeading [^]

(Completed) -> [CLASS]
(Completed)    (PROCEDURE | FUNCTION | CONSTRUCTOR | DESTRUCTOR | OPERATOR)
(Completed)    QualifiedIdent
(Completed)    (
(Completed)    ['(' (Parameter [';'])* ')']
(Completed)    [':' MethodReturnType]
(Completed)    (Directive)*
(Completed)    | '=' Ident
(Completed)    )
(Completed)    [';']

MethodImplementation [^]

If the MethodHeading does not include 'external' or 'forward':

(Completed) -> MethodHeading
(Completed)    FancyBlock ';'

If the MethodHeading does include 'external' or 'forward':

(Completed) -> MethodHeading

MethodOrProperty [^]

(Completed) -> MethodHeading
(Completed) -> Property

MethodReturnType [^]

(Completed) -> QualifiedIdent
(Completed) -> STRING

MulOp [^]

(Completed) -> '*'
(Completed) -> '/'
(Completed) -> DIV
(Completed) -> MOD
(Completed) -> AND
(Completed) -> SHL
(Completed) -> SHR

OpenArray [^]

(Completed) -> ARRAY OF QualifiedIdent
(Completed) -> ARRAY OF STRING
(Completed) -> ARRAY OF FILE
(Completed) -> ARRAY OF CONST

Package [^]

(Completed) -> PACKAGE QualifiedIdent ';'
(Completed)    [RequiresClause]
(Completed)    [UsesClause]
(Completed)    (AssemblyAttribute)*
(Completed)    END '.'

PackedType [^]

(Completed) -> PACKED Type

Parameter [^]

(Completed) -> [VAR | CONST | OUT]
(Completed)    IdentList
(Completed)    [':' ParameterType]
(Completed)    ['=' Expression]

ParameterExpression [^]

(Completed) -> Expression [':' Expression [':' Expression]]

ParameterType [^]

(Completed) -> QualifiedIdent
(Completed) -> STRING
(Completed) -> FILE
(Completed) -> OpenArray

ParenthesizedExpression [^]

(Completed) -> '(' Expression ')'

Particle [^]

(Completed) -> <number>
(Completed) -> <stringliteral>
(Completed) -> Ident
(Completed) -> NIL
(Completed) -> ParenthesizedExpression
(Completed) -> SetLiteral
(Completed) -> STRING
(Completed) -> FILE

PointerType [^]

(Completed) -> '^' Type

PortabilityDirective [^]

(Completed) -> platform
(Completed) -> deprecated
(Completed) -> library
(Completed) -> experimental

ProcedureType [^]

(Completed) -> (PROCEDURE | FUNCTION)
(Completed)    ['(' (Parameter [';'])* ')']
(Completed)    [':' MethodReturnType]
(Completed)    (Directive)*
(Completed)    [OF OBJECT]
(Completed)    (Directive)*

Program [^]

(Completed) -> (PROGRAM | LIBRARY) Ident ['(' IdentList ')'] ';'
(Completed) -> [UsesClause]
(Completed)    (ImplementationDecl)*
(Completed)    InitSection '.'

Property [^]

(Completed) -> [CLASS]
(Completed)    PROPERTY Ident
(Completed)    ['[' (Parameter [';'])+ ']']
(Completed)    [':' MethodReturnType]
(Completed)    (PropertyDirective)*
(Completed)    ';'

PropertyDirective [^]

(Completed) -> ';' DEFAULT
(Completed) -> DEFAULT Expression
(Completed) -> DISPID Expression
(Completed) -> IMPLEMENTS (QualifiedIdent [','])+
(Completed) -> INDEX Expression
(Completed) -> NODEFAULT
(Completed) -> READ Expression
(Completed) -> READONLY
(Completed) -> STORED Expression
(Completed) -> WRITE Expression
(Completed) -> WRITEONLY

QualifiedIdent [^]

(Completed) -> Ident ('.' ExtendedIdent)*

RaiseStatement [^]

(Completed) -> RAISE [Expression [AT Expression]]

RecordHelperType [^]

(Completed) -> RECORD HELPER FOR QualifiedIdent
(Completed)    (VisibilitySection)*
(Completed)    END

RecordType [^]

(Completed) -> RECORD
(Completed)    (VisibilitySection)*
(Completed)    [VariantSection]
(Completed)    END

RelOp [^]

(Completed) -> '='
(Completed) -> '>'
(Completed) -> '<'
(Completed) -> '<='
(Completed) -> '>='
(Completed) -> '<>'
(Completed) -> IN
(Completed) -> IS
(Completed) -> AS

RepeatStatement [^]

(Completed) -> REPEAT [StatementList] UNTIL Expression

RequiresClause [^]

(Completed) -> REQUIRES (QualifiedIdent [','])+ ';'

SetLiteral [^]

(Completed) -> '[' [ExpressionOrRangeList] ']'

SetType [^]

(Completed) -> SET OF Type

SimpleExpression [^]

(Completed) -> Term (AddOp Term)*

SimpleStatement [^]

(Completed) -> BareInherited
(Completed) -> ExpressionOrAssignment
(Completed) -> GotoStatement
(Completed) -> Block
(Completed) -> IfStatement
(Completed) -> CaseStatement
(Completed) -> RepeatStatement
(Completed) -> WhileStatement
(Completed) -> ForStatement
(Completed) -> WithStatement
(Completed) -> TryStatement
(Completed) -> RaiseStatement

Statement [^]

(Completed) -> LabelId ':' [SimpleStatement]
(Completed) -> SimpleStatement

StatementList [^]

(Completed) -> ([Statement] [';'])+

StringType [^]

(Completed) -> STRING
(Completed) -> STRING '[' Expression ']'

Term [^]

(Completed) -> Factor (MulOp Factor)*

TryStatement [^]

(Completed) -> TRY
(Completed)    [StatementList]
(Completed)    ( FINALLY [StatementList]
(Completed)    | EXCEPT (
(Completed)    [StatementList] |
(Completed)    (ExceptionItem)* [ELSE [StatementList]]
(Completed)    )
(Completed)    END

Type [^]

Note: Delphi assumes that a Type starting with '(' is an enum, not an expression.

(Completed) -> EnumeratedType
(Completed) -> ExpressionOrRange
(Completed) -> ArrayType
(Completed) -> SetType
(Completed) -> FileType
(Completed) -> RecordHelperType
(Completed) -> RecordType
(Completed) -> PointerType
(Completed) -> StringType
(Completed) -> ProcedureType
(Completed) -> ClassHelperType
(Completed) -> ClassOfType
(Completed) -> ClassType
(Completed) -> InterfaceType
(Completed) -> PackedType

TypedConstant [^]

(Completed) -> Expression
(Completed) -> '(' (QualifiedIdent ':' TypedConstant [';'])+ ')'
(Completed) -> '(' (TypedConstant [','])+ ')'
(Completed) -> '(' ')'

TypeDecl [^]

(Completed) -> Ident '=' [TYPE] Type (PortabilityDirective)* ';'
(Completed) -> Ident '=' CLASS ';'
(Completed) -> Ident '=' DISPINTERFACE ';'
(Completed) -> Ident '=' INTERFACE ';'

TypeSection [^]

(Completed) -> TYPE (TypeDecl)+

UnaryOperator [^]

(Completed) -> NOT
(Completed) -> '+'
(Completed) -> '-'
(Completed) -> '@'
(Completed) -> INHERITED

Unit [^]

(Completed) -> UNIT Ident (PortabilityDirective)* ';'
(Completed)    InterfaceSection
(Completed)    ImplementationSection
(Completed)    InitSection '.'

UsedUnit [^]

(Completed) -> Ident
(Completed) -> Ident IN <stringliteral>

UsesClause [^]

(Completed) -> (USES | CONTAINS)
(Completed)    (UsedUnit [','])+ ';'

VarDecl [^]

(Completed) -> IdentList ':' Type
(Completed)    (PortabilityDirective)*
(Completed)    [ABSOLUTE Expression | '=' TypedConstant]
(Completed)    (PortabilityDirective)*
(Completed)    ';'

VariantGroup [^]

(Completed) -> ExpressionList ':'
(Completed)    '('
(Completed) -> (FieldDecl)*
(Completed)    [VariantSection]
(Completed)    ')' [';']

VariantSection [^]

(Completed) -> CASE [Ident ':'] QualifiedIdent OF
(Completed)    (VariantGroup)+

VarSection [^]

(Completed) -> (VAR | THREADVAR) (VarDecl)+

Visibility [^]

(Completed) -> STRICT PRIVATE
(Completed) -> STRICT PROTECTED
(Completed) -> PRIVATE
(Completed) -> PROTECTED
(Completed) -> PUBLIC
(Completed) -> PUBLISHED

VisibilitySection [^]

(Completed) -> [Visibility]
(Completed)    (VisibilitySectionContent)*

VisibilitySectionContent [^]

(Completed) -> FieldSection
(Completed) -> MethodOrProperty
(Completed) -> ConstSection
(Completed) -> TypeSection

WhileStatement [^]

(Completed) -> WHILE Expression DO [Statement]

WithStatement [^]

(Completed) -> WITH ExpressionList DO [Statement]