free_pascal.atg 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. (* Free Pascal ATG for Coco/R *)
  2. (* Adapted from the standard Pascal grammar *)
  3. (* Tags: FREEPASCAL, FP, FPC, DELPHI *)
  4. COMPILER FPC
  5. IGNORE CASE
  6. CHARACTERS
  7. eol = CHR(10) .
  8. letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  9. digit = "0123456789" .
  10. hexDigit = digit + "ABCDEF" .
  11. octDigit = "01234567" .
  12. binDigit = "01" .
  13. noQuote1 = ANY - "'" - eol .
  14. IGNORE CHR(9) .. CHR(13) + CHR(239) + CHR(187) + CHR(191)
  15. COMMENTS FROM "(*" TO "*)" NESTED
  16. COMMENTS FROM "{" TO "}"
  17. COMMENTS FROM "//" TO eol
  18. COMMENTS FROM "{$" TO "}"
  19. TOKENS
  20. ident = ( letter | "_" ) { letter | digit | "_" } .
  21. pnumber = digit { digit } | digit { digit } CONTEXT ( ".." )
  22. | "$" hexDigit { hexDigit }
  23. | "&" octDigit { octDigit }
  24. | "%" binDigit { binDigit } .
  25. preal = digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] .
  26. pstring = "'" { noQuote1 | "''" } "'" .
  27. PRODUCTIONS
  28. FPC = program | library | Unit | Fragment .
  29. Fragment = { LabelDecl | ConstDecl | TypeDecl | VarDecl | ProcDecl | Compound | UsesClause } [ "." ] .
  30. library = "library" UnitId ";" [ UsesClause ] Block "." .
  31. program = "program" UnitId ";" [ UsesClause ] Block "." .
  32. Unit = "unit" UnitId ";" InterfaceSection [ ImplementationSection ]
  33. [ [ InitializationSection ] [ FinalizationSection ] "end" | Compound ] "." .
  34. InterfaceSection = "interface" [ UsesClause ] { ConstDecl | TypeDecl | VarDecl | ThreadVar | ResStr | MethodSig } .
  35. ImplementationSection = "implementation" [ UsesClause ]
  36. { ConstDecl | TypeDecl | VarDecl | ThreadVar | ResStr | ProcDecl | Compound [";"] } .
  37. InitializationSection = "initialization" Statements .
  38. FinalizationSection = "finalization" Statements .
  39. UsesClause = "uses" UnitName { [ "," ] UnitName } ";" .
  40. UnitName = NameComp { "." NameComp } .
  41. NameComp = ident | "overload" | "varargs" | "external" | "set" | "threadvar" | "static"
  42. | "and" | "or" | "xor" | "div" | "mod" | "not" | "shl" | "shr" | "sar" | "in" | "is" | "as"
  43. | "helper" | "far" | "near" | "cvar" | "public" | "export" | "register"
  44. | "sealed" | "abstract" | "bitpacked" | "object" | "objcclass" .
  45. UnitId = ident | "helper" | "far" | "near" | "cvar" | "public" | "export" | "register"
  46. | "sealed" | "abstract" | "bitpacked" | "object" | "objcclass" .
  47. MethodSig = [ "generic" ] ( "procedure" | "function" | "constructor" | "destructor" ) XIdent [ "<" TypeParamList GClose ] [ FormalParams ] [ ":" Type ] ";" { MethodDir ";" } .
  48. TypeParamList = ConstrParam { "," ConstrParam } .
  49. ConstrParam = ident [ ":" ( "class" | "record" | "constructor" | TypeIdent ) ] .
  50. GClose = ">" | ">=" .
  51. TypeArgList = Type { "," Type } .
  52. GenericName = ident [ "<" TypeArgList GClose ] .
  53. Block = DeclPart [ Compound ] .
  54. Compound = "begin" Statements "end" .
  55. DeclPart = { LabelDecl | ConstDecl | TypeDecl | VarDecl | ThreadVar | ResStr | ProcDecl } .
  56. ResStr = "resourcestring" [ ResItem { ";" [ ResItem | JuxValue ] } ] .
  57. ResItem = XIdent "=" ( pstring | CharLit ) { "+" | pstring | CharLit } .
  58. ThreadVar = "threadvar" [ VDecl { ";" [ VDecl ] } ] .
  59. LabelDecl = "label" Label { "," Label } ";" .
  60. ConstDecl = "const" [ CDecl { ";" [ CDecl | JuxValue ] } ] .
  61. JuxValue = pnumber | preal | StrChain | "nil" .
  62. CDecl = XIdent [ ":" Type ] "=" Value .
  63. Value = Expr .
  64. TypeDecl = "type" [ TypeDef { ";" { TailDir ";" } [ TypeDef ] } ] .
  65. TypeDef = [ "generic" ] ident TypeDefTail .
  66. TypeDefTail = "=" Type | "<" TypeParamList ( ">" "=" Type | ">=" Type ) .
  67. VarDecl = "var" [ VDecl { ";" { TailDir ";" } [ VDecl ] } ] .
  68. VarDeclNoTail = "var" [ VDecl { ";" [ VDecl ] } ] .
  69. TailDir = "far" | "near" | "cvar" | "public" | "export" | "register" | "cdecl" | "stdcall" | "pascal" | "safecall" | "extdecl" | ExternalDir .
  70. VDecl = varlist ":" Type [ "absolute" ( ident { Selector } | "(" Expr ")" ) ] [ "=" Value ] .
  71. varlist = XIdent { "," XIdent } .
  72. XIdent = ident | "out" | "static" | "register" | "far" | "near" .
  73. identlist = ident { "," ident } .
  74. Type = SimpleType | StructType | PointerType | EnumType | ProceduralType | TypeIdent | "type" [ "helper" "for" ] Type .
  75. ProceduralType = ( "function" [ FormalParams ] ":" Type | "procedure" [ FormalParams ] ) [ "of" ( ident | "object" ) ] .
  76. EnumType = "(" EnumMember { "," EnumMember } ")" .
  77. EnumMember = ident [ ( "=" | ":=" ) Expr ] .
  78. TypeIdent = ident { "." ident } [ "<" TypeArgList GClose ] [ "(" Expr ")" | "[" Expr "]" ] [ ".." Expr ] | [ "-" ] pnumber [ ".." Expr ]
  79. | "specialize" ident { "." ident } "<" TypeArgList GClose .
  80. PointerType = "^" Type .
  81. SimpleType = IntType | RealType | CharType | BoolType .
  82. IntType = "integer" .
  83. RealType = "real" .
  84. CharType = "char" .
  85. BoolType = "boolean" .
  86. StructType = [ "packed" | "bitpacked" | "X_PACKED" ] ( ArrayType | RecordType ) | SetType | FileOfType | ClassType | ObjectType | ObjcClass | InterfaceType .
  87. ObjectType = "object" [ "(" GenericName ")" ] { ClassMember } "end" .
  88. ObjcClass = "objcclass" [ "external" ] [ "(" [ GenericName { "," GenericName } ] ")" ] [ { ClassMember } "end" ] .
  89. ArrayType = "array" [ "[" Bound { "," Bound } "]" ] "of" ( Type | "const" ) .
  90. Bound = SimpleType | Expr [ ".." Expr ] .
  91. RecordType = "record" [ "helper" "for" Type ] FieldList "end" .
  92. FieldList = [ Field { ";" [ Field ] | Field } ] .
  93. Field = VariantPart | [ VisSection [ ";" ] ] [ FieldListPart ":" Type | [ "class" ] ( MethodDecl | OperatorBody | PropertyDecl | VarDeclNoTail ) | ConstDecl | TypeDecl | VarDeclNoTail ] .
  94. VariantPart = "case" Type [ ":" Type ] "of" [ Variant { ";" [ Variant ] } ] .
  95. Variant = Labels ":" "(" FieldList ")" .
  96. FieldListPart = XIdent { "," XIdent } .
  97. SetType = "set" "of" Type .
  98. FileOfType = "file" [ "of" Type ] .
  99. ClassType = "class" ( "of" Type | { "sealed" | "abstract" } [ HelperFor | ParentList ] [ { ClassMember } "end" ] ) .
  100. ParentList = "(" [ "specialize" ] GenericName { "," [ "specialize" ] GenericName } ")" .
  101. HelperFor = "helper" [ ParentList ] "for" Type .
  102. ClassMember = VisSection | FieldDecl | ConstDecl | TypeDecl | VarDeclNoTail
  103. | "generic" [ "class" ] ( MethodDecl | PropertyDecl | OperatorBody | VarDeclNoTail )
  104. | "class" ( MethodDecl | PropertyDecl | OperatorBody | VarDeclNoTail )
  105. | MethodDecl | PropertyDecl | OperatorBody .
  106. OperatorBody = "operator" ( ident [ "." OpName ] | OpSymbol ) [ FormalParams ] [ ":" Type ] ";" { MethodDir ";" } .
  107. VisSection = [ "strict" ] ( "private" | "protected" | "public" | "published" ) .
  108. FieldDecl = identlist ":" Type ";" .
  109. MethodDecl = ( "procedure" | "function" | "constructor" | "destructor" ) XIdent [ "<" TypeParamList GClose ] [ "." ident ]
  110. [ FormalParams ] [ ":" Type ] ";" { MethodDir ";" } .
  111. MethodDir = "virtual" | "override" | "abstract" | "reintroduce" | "overload"
  112. | "cdecl" | "stdcall" | "inline" | "final" | "sealed" | "dynamic"
  113. | "deprecated" | "static" | "assembler" | "nostackframe" | SyscallDir | ExternalDir | ident .
  114. InhCall = "inherited" [ ident [ "(" [ ParamList ] ")" ] { Selector } ] .
  115. ExternalDir = "external" { pstring | ident | pnumber } .
  116. PropertyDecl = "property" XIdent [ "[" [ "const" | "var" ] ident ":" Type "]" ] [ ":" Type ]
  117. [ ( ident | pnumber ) { ident | pnumber } ] ";" { "deprecated" ";" } .
  118. InterfaceType = "interface" [ "(" ident ")" ] [ "[" ( pstring | ident ) "]" ] { MethodSig | PropertyDecl } "end" .
  119. ProcDecl = [ "generic" ] [ "class" ] ProcKind ";" { NonBodylessDir ";" }
  120. [ BodylessDir ";" | Block [ ";" ] ] .
  121. ProcKind = ( "procedure" | "function" | "constructor" | "destructor" ) XIdent [ "<" TypeParamList GClose ] [ "." ident ] [ FormalParams ] [ ":" Type ]
  122. | "operator" ( ident [ "." OpName ] | OpSymbol ) [ FormalParams ] [ ":" Type ] .
  123. OpSymbol = "+" | "-" | "*" | "/" | "div" | "mod" | "and" | "or" | "xor" | "shl" | "shr" | "not" | "=" | "<>" | "<" | ">" | "<=" | ">=" | ":=" .
  124. OpName = ident | OpSymbol .
  125. NonBodylessDir = "virtual" | "override" | "reintroduce"
  126. | "overload" | "varargs"
  127. | "cdecl" | "stdcall" | "pascal" | "safecall"
  128. | "inline" | "final" | "sealed" | "dynamic" | "deprecated"
  129. | "static" | "far" | "near" | "assembler" | "nostackframe" .
  130. BodylessDir = "forward" | "abstract" | SyscallDir | ExternalDir .
  131. SyscallDir = "syscall" [ ident ] .
  132. FormalParams = "(" [ FormalGroup { ";" FormalGroup } ] ")" .
  133. FormalGroup = [ "var" | "out" | "const" ] identlist [ ":" Type ] [ "=" DefaultValue ] .
  134. DefaultValue = [ "-" ] ( pnumber | preal ) | pstring | "nil" | XIdent | "[" [ SimpleExpr { "," SimpleExpr } ] "]" .
  135. Statements = [ Statement { ";" [ Statement ] } ] .
  136. Statement = [ pnumber ":" ]
  137. ( IfStmt
  138. | CaseStmt
  139. | WhileStmt
  140. | RepeatStmt
  141. | ForStmt
  142. | XIdent [ IdentTail ]
  143. | RetStmt
  144. | WithStmt
  145. | GotoStmt
  146. | TryStmt
  147. | RaiseStmt
  148. | ExitStmt
  149. | "break"
  150. | "continue"
  151. | Compound
  152. | InhCall
  153. | ProcDecl ) .
  154. IfStmt = "if" Expr "then" [ Statement ] { "else" [ Statement ] } .
  155. CaseStmt = "case" Expr "of" [ CaseItem { ";" [ CaseItem ] } ] [ "else" Statements ] "end" .
  156. CaseItem = Labels ":" [ Statement ] .
  157. Labels = CaseLabel { "," CaseLabel } .
  158. Label = pnumber | ident .
  159. CaseLabel = CaseBound [ ".." CaseBound ] .
  160. CaseBound = pnumber | ident { "." ident } | pstring | CharLit .
  161. WhileStmt = "while" Expr "do" [ Statement ] .
  162. RepeatStmt = "repeat" Statements "until" Expr .
  163. ForStmt = "for" XIdent ( ":=" Expr ( "to" | "downto" ) Expr | "in" Expr ) "do" [ Statement ] .
  164. TryStmt = "try" Statements ( "finally" Statements | "except" ExceptBody ) "end" .
  165. ExceptBody = Statements [ "else" Statements ] .
  166. RaiseStmt = "raise" [ Expr ] .
  167. ExitStmt = "exit" [ "(" Expr ")" ] .
  168. IdentTail = ":=" Expr
  169. | ( "+=" | "-=" | "*=" | "/=" ) Expr
  170. | ":" Statement
  171. | PostfixBody [ ":=" Expr ]
  172. | ident [ ":" ident { "." ident } ] "do" [ Statement ] .
  173. PostfixBody = ( Selector | "<" GenArgs [ GClose ] | "(" [ ParamList ] ")" ) Postfix .
  174. Postfix = { Selector | "<" GenArgs [ GClose ] | "(" [ ParamList ] ")" } .
  175. GenArgs = SimpleExpr { "," SimpleExpr } .
  176. ParamList = Param { "," Param } .
  177. Param = Expr [ ":" Expr [ ":" Expr ] ] .
  178. RetStmt = "return" [ Expr ] .
  179. WithStmt = "with" WithItem { "," WithItem } "do" [ Statement ] .
  180. WithItem = ident Postfix .
  181. GotoStmt = "goto" Label .
  182. Expr = SimpleExpr [ ( Relationship | "in" ) SimpleExpr ] [ ( "is" | "as" ) ident ] .
  183. Relationship = "=" | "#" | "<" | ">" | "<=" | ">=" | "<>" .
  184. SimpleExpr = ["+" | "-"] Term { ("+" | "-" | "or" | "xor") Term } .
  185. Term = Factor { ("**" | "*" | "/" | "div" | "mod" | "&" | "and" | "shl" | "shr" | "sar") Factor } .
  186. Factor = ["@" | "+" | "-"] ["not"] Primary .
  187. Primary = pnumber | preal | StrChain | "nil" | InhCall | BuiltinCast | "[" [ SetElem { "," SetElem } ] "]" | "(" [ PItem { ( "," | ";" ) [ PItem ] } ] ")" { "^" { Selector } } | XIdent Postfix .
  188. BuiltinCast = ( IntType | RealType | CharType | BoolType ) "(" [ ParamList ] ")" .
  189. PItem = Expr [ ":" Expr ] .
  190. StrChain = ( pstring | CharLit ) { pstring | CharLit } .
  191. SetElem = Expr [ ".." Expr ] .
  192. CharLit = ( "#" pnumber | "^" ident ) { "#" pnumber | "^" ident } .
  193. Selector = "." XIdent | "[" Expr { ( "," | ":" ) Expr } "]" | "^" .
  194. END FPC.