pegs.nim 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Simple PEG (Parsing expression grammar) matching. Uses no memorization, but
  10. ## uses superoperators and symbol inlining to improve performance. Note:
  11. ## Matching performance is hopefully competitive with optimized regular
  12. ## expression engines.
  13. ##
  14. ## .. include:: ../../doc/pegdocs.txt
  15. ##
  16. include "system/inclrtl"
  17. const
  18. useUnicode = true ## change this to deactivate proper UTF-8 support
  19. import strutils, macros
  20. when useUnicode:
  21. import unicode
  22. export unicode.`==`
  23. const
  24. InlineThreshold = 5 ## number of leaves; -1 to disable inlining
  25. MaxSubpatterns* = 20 ## defines the maximum number of subpatterns that
  26. ## can be captured. More subpatterns cannot be captured!
  27. type
  28. PegKind* = enum
  29. pkEmpty,
  30. pkAny, ## any character (.)
  31. pkAnyRune, ## any Unicode character (_)
  32. pkNewLine, ## CR-LF, LF, CR
  33. pkLetter, ## Unicode letter
  34. pkLower, ## Unicode lower case letter
  35. pkUpper, ## Unicode upper case letter
  36. pkTitle, ## Unicode title character
  37. pkWhitespace, ## Unicode whitespace character
  38. pkTerminal,
  39. pkTerminalIgnoreCase,
  40. pkTerminalIgnoreStyle,
  41. pkChar, ## single character to match
  42. pkCharChoice,
  43. pkNonTerminal,
  44. pkSequence, ## a b c ... --> Internal DSL: peg(a, b, c)
  45. pkOrderedChoice, ## a / b / ... --> Internal DSL: a / b or /[a, b, c]
  46. pkGreedyRep, ## a* --> Internal DSL: *a
  47. ## a+ --> (a a*)
  48. pkGreedyRepChar, ## x* where x is a single character (superop)
  49. pkGreedyRepSet, ## [set]* (superop)
  50. pkGreedyAny, ## .* or _* (superop)
  51. pkOption, ## a? --> Internal DSL: ?a
  52. pkAndPredicate, ## &a --> Internal DSL: &a
  53. pkNotPredicate, ## !a --> Internal DSL: !a
  54. pkCapture, ## {a} --> Internal DSL: capture(a)
  55. pkBackRef, ## $i --> Internal DSL: backref(i)
  56. pkBackRefIgnoreCase,
  57. pkBackRefIgnoreStyle,
  58. pkSearch, ## @a --> Internal DSL: !*a
  59. pkCapturedSearch, ## {@} a --> Internal DSL: !*\a
  60. pkRule, ## a <- b
  61. pkList, ## a, b
  62. pkStartAnchor ## ^ --> Internal DSL: startAnchor()
  63. NonTerminalFlag* = enum
  64. ntDeclared, ntUsed
  65. NonTerminalObj = object ## represents a non terminal symbol
  66. name: string ## the name of the symbol
  67. line: int ## line the symbol has been declared/used in
  68. col: int ## column the symbol has been declared/used in
  69. flags: set[NonTerminalFlag] ## the nonterminal's flags
  70. rule: Peg ## the rule that the symbol refers to
  71. Peg* {.shallow.} = object ## type that represents a PEG
  72. case kind: PegKind
  73. of pkEmpty..pkWhitespace: nil
  74. of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle: term: string
  75. of pkChar, pkGreedyRepChar: ch: char
  76. of pkCharChoice, pkGreedyRepSet: charChoice: ref set[char]
  77. of pkNonTerminal: nt: NonTerminal
  78. of pkBackRef..pkBackRefIgnoreStyle: index: range[0..MaxSubpatterns]
  79. else: sons: seq[Peg]
  80. NonTerminal* = ref NonTerminalObj
  81. proc kind*(p: Peg): PegKind = p.kind
  82. ## Returns the *PegKind* of a given *Peg* object.
  83. proc term*(p: Peg): string = p.term
  84. ## Returns the *string* representation of a given *Peg* variant object
  85. ## where present.
  86. proc ch*(p: Peg): char = p.ch
  87. ## Returns the *char* representation of a given *Peg* variant object
  88. ## where present.
  89. proc charChoice*(p: Peg): ref set[char] = p.charChoice
  90. ## Returns the *charChoice* field of a given *Peg* variant object
  91. ## where present.
  92. proc nt*(p: Peg): NonTerminal = p.nt
  93. ## Returns the *NonTerminal* object of a given *Peg* variant object
  94. ## where present.
  95. proc index*(p: Peg): range[0..MaxSubpatterns] = p.index
  96. ## Returns the back-reference index of a captured sub-pattern in the
  97. ## *Captures* object for a given *Peg* variant object where present.
  98. iterator items*(p: Peg): Peg {.inline.} =
  99. ## Yields the child nodes of a *Peg* variant object where present.
  100. for s in p.sons:
  101. yield s
  102. iterator pairs*(p: Peg): (int, Peg) {.inline.} =
  103. ## Yields the indices and child nodes of a *Peg* variant object where present.
  104. for i in 0 ..< p.sons.len:
  105. yield (i, p.sons[i])
  106. proc name*(nt: NonTerminal): string = nt.name
  107. ## Gets the name of the symbol represented by the parent *Peg* object variant
  108. ## of a given *NonTerminal*.
  109. proc line*(nt: NonTerminal): int = nt.line
  110. ## Gets the line number of the definition of the parent *Peg* object variant
  111. ## of a given *NonTerminal*.
  112. proc col*(nt: NonTerminal): int = nt.col
  113. ## Gets the column number of the definition of the parent *Peg* object variant
  114. ## of a given *NonTerminal*.
  115. proc flags*(nt: NonTerminal): set[NonTerminalFlag] = nt.flags
  116. ## Gets the *NonTerminalFlag*-typed flags field of the parent *Peg* variant
  117. ## object of a given *NonTerminal*.
  118. proc rule*(nt: NonTerminal): Peg = nt.rule
  119. ## Gets the *Peg* object representing the rule definition of the parent *Peg*
  120. ## object variant of a given *NonTerminal*.
  121. proc term*(t: string): Peg {.nosideEffect, rtl, extern: "npegs$1Str".} =
  122. ## constructs a PEG from a terminal string
  123. if t.len != 1:
  124. result.kind = pkTerminal
  125. result.term = t
  126. else:
  127. result.kind = pkChar
  128. result.ch = t[0]
  129. proc termIgnoreCase*(t: string): Peg {.
  130. nosideEffect, rtl, extern: "npegs$1".} =
  131. ## constructs a PEG from a terminal string; ignore case for matching
  132. result.kind = pkTerminalIgnoreCase
  133. result.term = t
  134. proc termIgnoreStyle*(t: string): Peg {.
  135. nosideEffect, rtl, extern: "npegs$1".} =
  136. ## constructs a PEG from a terminal string; ignore style for matching
  137. result.kind = pkTerminalIgnoreStyle
  138. result.term = t
  139. proc term*(t: char): Peg {.nosideEffect, rtl, extern: "npegs$1Char".} =
  140. ## constructs a PEG from a terminal char
  141. assert t != '\0'
  142. result.kind = pkChar
  143. result.ch = t
  144. proc charSet*(s: set[char]): Peg {.nosideEffect, rtl, extern: "npegs$1".} =
  145. ## constructs a PEG from a character set `s`
  146. assert '\0' notin s
  147. result.kind = pkCharChoice
  148. new(result.charChoice)
  149. result.charChoice[] = s
  150. proc len(a: Peg): int {.inline.} = return a.sons.len
  151. proc add(d: var Peg, s: Peg) {.inline.} = add(d.sons, s)
  152. proc addChoice(dest: var Peg, elem: Peg) =
  153. var L = dest.len-1
  154. if L >= 0 and dest.sons[L].kind == pkCharChoice:
  155. # caution! Do not introduce false aliasing here!
  156. case elem.kind
  157. of pkCharChoice:
  158. dest.sons[L] = charSet(dest.sons[L].charChoice[] + elem.charChoice[])
  159. of pkChar:
  160. dest.sons[L] = charSet(dest.sons[L].charChoice[] + {elem.ch})
  161. else: add(dest, elem)
  162. else: add(dest, elem)
  163. template multipleOp(k: PegKind, localOpt: untyped) =
  164. result.kind = k
  165. result.sons = @[]
  166. for x in items(a):
  167. if x.kind == k:
  168. for y in items(x.sons):
  169. localOpt(result, y)
  170. else:
  171. localOpt(result, x)
  172. if result.len == 1:
  173. result = result.sons[0]
  174. proc `/`*(a: varargs[Peg]): Peg {.
  175. nosideEffect, rtl, extern: "npegsOrderedChoice".} =
  176. ## constructs an ordered choice with the PEGs in `a`
  177. multipleOp(pkOrderedChoice, addChoice)
  178. proc addSequence(dest: var Peg, elem: Peg) =
  179. var L = dest.len-1
  180. if L >= 0 and dest.sons[L].kind == pkTerminal:
  181. # caution! Do not introduce false aliasing here!
  182. case elem.kind
  183. of pkTerminal:
  184. dest.sons[L] = term(dest.sons[L].term & elem.term)
  185. of pkChar:
  186. dest.sons[L] = term(dest.sons[L].term & elem.ch)
  187. else: add(dest, elem)
  188. else: add(dest, elem)
  189. proc sequence*(a: varargs[Peg]): Peg {.
  190. nosideEffect, rtl, extern: "npegs$1".} =
  191. ## constructs a sequence with all the PEGs from `a`
  192. multipleOp(pkSequence, addSequence)
  193. proc `?`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsOptional".} =
  194. ## constructs an optional for the PEG `a`
  195. if a.kind in {pkOption, pkGreedyRep, pkGreedyAny, pkGreedyRepChar,
  196. pkGreedyRepSet}:
  197. # a* ? --> a*
  198. # a? ? --> a?
  199. result = a
  200. else:
  201. result.kind = pkOption
  202. result.sons = @[a]
  203. proc `*`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsGreedyRep".} =
  204. ## constructs a "greedy repetition" for the PEG `a`
  205. case a.kind
  206. of pkGreedyRep, pkGreedyRepChar, pkGreedyRepSet, pkGreedyAny, pkOption:
  207. assert false
  208. # produces endless loop!
  209. of pkChar:
  210. result.kind = pkGreedyRepChar
  211. result.ch = a.ch
  212. of pkCharChoice:
  213. result.kind = pkGreedyRepSet
  214. result.charChoice = a.charChoice # copying a reference suffices!
  215. of pkAny, pkAnyRune:
  216. result.kind = pkGreedyAny
  217. else:
  218. result.kind = pkGreedyRep
  219. result.sons = @[a]
  220. proc `!*`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsSearch".} =
  221. ## constructs a "search" for the PEG `a`
  222. result.kind = pkSearch
  223. result.sons = @[a]
  224. proc `!*\`*(a: Peg): Peg {.noSideEffect, rtl,
  225. extern: "npgegsCapturedSearch".} =
  226. ## constructs a "captured search" for the PEG `a`
  227. result.kind = pkCapturedSearch
  228. result.sons = @[a]
  229. proc `+`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsGreedyPosRep".} =
  230. ## constructs a "greedy positive repetition" with the PEG `a`
  231. return sequence(a, *a)
  232. proc `&`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsAndPredicate".} =
  233. ## constructs an "and predicate" with the PEG `a`
  234. result.kind = pkAndPredicate
  235. result.sons = @[a]
  236. proc `!`*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsNotPredicate".} =
  237. ## constructs a "not predicate" with the PEG `a`
  238. result.kind = pkNotPredicate
  239. result.sons = @[a]
  240. proc any*: Peg {.inline.} =
  241. ## constructs the PEG `any character`:idx: (``.``)
  242. result.kind = pkAny
  243. proc anyRune*: Peg {.inline.} =
  244. ## constructs the PEG `any rune`:idx: (``_``)
  245. result.kind = pkAnyRune
  246. proc newLine*: Peg {.inline.} =
  247. ## constructs the PEG `newline`:idx: (``\n``)
  248. result.kind = pkNewLine
  249. proc unicodeLetter*: Peg {.inline.} =
  250. ## constructs the PEG ``\letter`` which matches any Unicode letter.
  251. result.kind = pkLetter
  252. proc unicodeLower*: Peg {.inline.} =
  253. ## constructs the PEG ``\lower`` which matches any Unicode lowercase letter.
  254. result.kind = pkLower
  255. proc unicodeUpper*: Peg {.inline.} =
  256. ## constructs the PEG ``\upper`` which matches any Unicode uppercase letter.
  257. result.kind = pkUpper
  258. proc unicodeTitle*: Peg {.inline.} =
  259. ## constructs the PEG ``\title`` which matches any Unicode title letter.
  260. result.kind = pkTitle
  261. proc unicodeWhitespace*: Peg {.inline.} =
  262. ## constructs the PEG ``\white`` which matches any Unicode
  263. ## whitespace character.
  264. result.kind = pkWhitespace
  265. proc startAnchor*: Peg {.inline.} =
  266. ## constructs the PEG ``^`` which matches the start of the input.
  267. result.kind = pkStartAnchor
  268. proc endAnchor*: Peg {.inline.} =
  269. ## constructs the PEG ``$`` which matches the end of the input.
  270. result = !any()
  271. proc capture*(a: Peg): Peg {.nosideEffect, rtl, extern: "npegsCapture".} =
  272. ## constructs a capture with the PEG `a`
  273. result.kind = pkCapture
  274. result.sons = @[a]
  275. proc backref*(index: range[1..MaxSubpatterns]): Peg {.
  276. nosideEffect, rtl, extern: "npegs$1".} =
  277. ## constructs a back reference of the given `index`. `index` starts counting
  278. ## from 1.
  279. result.kind = pkBackRef
  280. result.index = index-1
  281. proc backrefIgnoreCase*(index: range[1..MaxSubpatterns]): Peg {.
  282. nosideEffect, rtl, extern: "npegs$1".} =
  283. ## constructs a back reference of the given `index`. `index` starts counting
  284. ## from 1. Ignores case for matching.
  285. result.kind = pkBackRefIgnoreCase
  286. result.index = index-1
  287. proc backrefIgnoreStyle*(index: range[1..MaxSubpatterns]): Peg {.
  288. nosideEffect, rtl, extern: "npegs$1".}=
  289. ## constructs a back reference of the given `index`. `index` starts counting
  290. ## from 1. Ignores style for matching.
  291. result.kind = pkBackRefIgnoreStyle
  292. result.index = index-1
  293. proc spaceCost(n: Peg): int =
  294. case n.kind
  295. of pkEmpty: discard
  296. of pkTerminal, pkTerminalIgnoreCase, pkTerminalIgnoreStyle, pkChar,
  297. pkGreedyRepChar, pkCharChoice, pkGreedyRepSet,
  298. pkAny..pkWhitespace, pkGreedyAny:
  299. result = 1
  300. of pkNonTerminal:
  301. # we cannot inline a rule with a non-terminal
  302. result = InlineThreshold+1
  303. else:
  304. for i in 0..n.len-1:
  305. inc(result, spaceCost(n.sons[i]))
  306. if result >= InlineThreshold: break
  307. proc nonterminal*(n: NonTerminal): Peg {.
  308. nosideEffect, rtl, extern: "npegs$1".} =
  309. ## constructs a PEG that consists of the nonterminal symbol
  310. assert n != nil
  311. if ntDeclared in n.flags and spaceCost(n.rule) < InlineThreshold:
  312. when false: echo "inlining symbol: ", n.name
  313. result = n.rule # inlining of rule enables better optimizations
  314. else:
  315. result.kind = pkNonTerminal
  316. result.nt = n
  317. proc newNonTerminal*(name: string, line, column: int): NonTerminal {.
  318. nosideEffect, rtl, extern: "npegs$1".} =
  319. ## constructs a nonterminal symbol
  320. new(result)
  321. result.name = name
  322. result.line = line
  323. result.col = column
  324. template letters*: Peg =
  325. ## expands to ``charset({'A'..'Z', 'a'..'z'})``
  326. charSet({'A'..'Z', 'a'..'z'})
  327. template digits*: Peg =
  328. ## expands to ``charset({'0'..'9'})``
  329. charSet({'0'..'9'})
  330. template whitespace*: Peg =
  331. ## expands to ``charset({' ', '\9'..'\13'})``
  332. charSet({' ', '\9'..'\13'})
  333. template identChars*: Peg =
  334. ## expands to ``charset({'a'..'z', 'A'..'Z', '0'..'9', '_'})``
  335. charSet({'a'..'z', 'A'..'Z', '0'..'9', '_'})
  336. template identStartChars*: Peg =
  337. ## expands to ``charset({'A'..'Z', 'a'..'z', '_'})``
  338. charSet({'a'..'z', 'A'..'Z', '_'})
  339. template ident*: Peg =
  340. ## same as ``[a-zA-Z_][a-zA-z_0-9]*``; standard identifier
  341. sequence(charSet({'a'..'z', 'A'..'Z', '_'}),
  342. *charSet({'a'..'z', 'A'..'Z', '0'..'9', '_'}))
  343. template natural*: Peg =
  344. ## same as ``\d+``
  345. +digits
  346. # ------------------------- debugging -----------------------------------------
  347. proc esc(c: char, reserved = {'\0'..'\255'}): string =
  348. case c
  349. of '\b': result = "\\b"
  350. of '\t': result = "\\t"
  351. of '\c': result = "\\c"
  352. of '\L': result = "\\l"
  353. of '\v': result = "\\v"
  354. of '\f': result = "\\f"
  355. of '\e': result = "\\e"
  356. of '\a': result = "\\a"
  357. of '\\': result = "\\\\"
  358. of 'a'..'z', 'A'..'Z', '0'..'9', '_': result = $c
  359. elif c < ' ' or c >= '\127': result = '\\' & $ord(c)
  360. elif c in reserved: result = '\\' & c
  361. else: result = $c
  362. proc singleQuoteEsc(c: char): string = return "'" & esc(c, {'\''}) & "'"
  363. proc singleQuoteEsc(str: string): string =
  364. result = "'"
  365. for c in items(str): add result, esc(c, {'\''})
  366. add result, '\''
  367. proc charSetEscAux(cc: set[char]): string =
  368. const reserved = {'^', '-', ']'}
  369. result = ""
  370. var c1 = 0
  371. while c1 <= 0xff:
  372. if chr(c1) in cc:
  373. var c2 = c1
  374. while c2 < 0xff and chr(succ(c2)) in cc: inc(c2)
  375. if c1 == c2:
  376. add result, esc(chr(c1), reserved)
  377. elif c2 == succ(c1):
  378. add result, esc(chr(c1), reserved) & esc(chr(c2), reserved)
  379. else:
  380. add result, esc(chr(c1), reserved) & '-' & esc(chr(c2), reserved)
  381. c1 = c2
  382. inc(c1)
  383. proc charSetEsc(cc: set[char]): string =
  384. if card(cc) >= 128+64:
  385. result = "[^" & charSetEscAux({'\1'..'\xFF'} - cc) & ']'
  386. else:
  387. result = '[' & charSetEscAux(cc) & ']'
  388. proc toStrAux(r: Peg, res: var string) =
  389. case r.kind
  390. of pkEmpty: add(res, "()")
  391. of pkAny: add(res, '.')
  392. of pkAnyRune: add(res, '_')
  393. of pkLetter: add(res, "\\letter")
  394. of pkLower: add(res, "\\lower")
  395. of pkUpper: add(res, "\\upper")
  396. of pkTitle: add(res, "\\title")
  397. of pkWhitespace: add(res, "\\white")
  398. of pkNewLine: add(res, "\\n")
  399. of pkTerminal: add(res, singleQuoteEsc(r.term))
  400. of pkTerminalIgnoreCase:
  401. add(res, 'i')
  402. add(res, singleQuoteEsc(r.term))
  403. of pkTerminalIgnoreStyle:
  404. add(res, 'y')
  405. add(res, singleQuoteEsc(r.term))
  406. of pkChar: add(res, singleQuoteEsc(r.ch))
  407. of pkCharChoice: add(res, charSetEsc(r.charChoice[]))
  408. of pkNonTerminal: add(res, r.nt.name)
  409. of pkSequence:
  410. add(res, '(')
  411. toStrAux(r.sons[0], res)
  412. for i in 1 .. high(r.sons):
  413. add(res, ' ')
  414. toStrAux(r.sons[i], res)
  415. add(res, ')')
  416. of pkOrderedChoice:
  417. add(res, '(')
  418. toStrAux(r.sons[0], res)
  419. for i in 1 .. high(r.sons):
  420. add(res, " / ")
  421. toStrAux(r.sons[i], res)
  422. add(res, ')')
  423. of pkGreedyRep:
  424. toStrAux(r.sons[0], res)
  425. add(res, '*')
  426. of pkGreedyRepChar:
  427. add(res, singleQuoteEsc(r.ch))
  428. add(res, '*')
  429. of pkGreedyRepSet:
  430. add(res, charSetEsc(r.charChoice[]))
  431. add(res, '*')
  432. of pkGreedyAny:
  433. add(res, ".*")
  434. of pkOption:
  435. toStrAux(r.sons[0], res)
  436. add(res, '?')
  437. of pkAndPredicate:
  438. add(res, '&')
  439. toStrAux(r.sons[0], res)
  440. of pkNotPredicate:
  441. add(res, '!')
  442. toStrAux(r.sons[0], res)
  443. of pkSearch:
  444. add(res, '@')
  445. toStrAux(r.sons[0], res)
  446. of pkCapturedSearch:
  447. add(res, "{@}")
  448. toStrAux(r.sons[0], res)
  449. of pkCapture:
  450. add(res, '{')
  451. toStrAux(r.sons[0], res)
  452. add(res, '}')
  453. of pkBackRef:
  454. add(res, '$')
  455. add(res, $r.index)
  456. of pkBackRefIgnoreCase:
  457. add(res, "i$")
  458. add(res, $r.index)
  459. of pkBackRefIgnoreStyle:
  460. add(res, "y$")
  461. add(res, $r.index)
  462. of pkRule:
  463. toStrAux(r.sons[0], res)
  464. add(res, " <- ")
  465. toStrAux(r.sons[1], res)
  466. of pkList:
  467. for i in 0 .. high(r.sons):
  468. toStrAux(r.sons[i], res)
  469. add(res, "\n")
  470. of pkStartAnchor:
  471. add(res, '^')
  472. proc `$` *(r: Peg): string {.nosideEffect, rtl, extern: "npegsToString".} =
  473. ## converts a PEG to its string representation
  474. result = ""
  475. toStrAux(r, result)
  476. # --------------------- core engine -------------------------------------------
  477. type
  478. Captures* = object ## contains the captured substrings.
  479. matches: array[0..MaxSubpatterns-1, tuple[first, last: int]]
  480. ml: int
  481. origStart: int
  482. {.deprecated: [TCaptures: Captures].}
  483. proc bounds*(c: Captures,
  484. i: range[0..MaxSubpatterns-1]): tuple[first, last: int] =
  485. ## returns the bounds ``[first..last]`` of the `i`'th capture.
  486. result = c.matches[i]
  487. when not useUnicode:
  488. type
  489. Rune = char
  490. template fastRuneAt(s, i, ch) =
  491. ch = s[i]
  492. inc(i)
  493. template runeLenAt(s, i): untyped = 1
  494. proc isAlpha(a: char): bool {.inline.} = return a in {'a'..'z','A'..'Z'}
  495. proc isUpper(a: char): bool {.inline.} = return a in {'A'..'Z'}
  496. proc isLower(a: char): bool {.inline.} = return a in {'a'..'z'}
  497. proc isTitle(a: char): bool {.inline.} = return false
  498. proc isWhiteSpace(a: char): bool {.inline.} = return a in {' ', '\9'..'\13'}
  499. template matchOrParse(mopProc: untyped): typed =
  500. # Used to make the main matcher proc *rawMatch* as well as event parser
  501. # procs. For the former, *enter* and *leave* event handler code generators
  502. # are provided which just return *discard*.
  503. proc mopProc(s: string, p: Peg, start: int, c: var Captures): int =
  504. proc matchBackRef(s: string, p: Peg, start: int, c: var Captures): int =
  505. # Parse handler code must run in an *of* clause of its own for each
  506. # *PegKind*, so we encapsulate the identical clause body for
  507. # *pkBackRef..pkBackRefIgnoreStyle* here.
  508. if p.index >= c.ml: return -1
  509. var (a, b) = c.matches[p.index]
  510. var n: Peg
  511. n.kind = succ(pkTerminal, ord(p.kind)-ord(pkBackRef))
  512. n.term = s.substr(a, b)
  513. mopProc(s, n, start, c)
  514. case p.kind
  515. of pkEmpty:
  516. enter(pkEmpty, s, p, start)
  517. result = 0 # match of length 0
  518. leave(pkEmpty, s, p, start, result)
  519. of pkAny:
  520. enter(pkAny, s, p, start)
  521. if start < s.len: result = 1
  522. else: result = -1
  523. leave(pkAny, s, p, start, result)
  524. of pkAnyRune:
  525. enter(pkAnyRune, s, p, start)
  526. if start < s.len:
  527. result = runeLenAt(s, start)
  528. else:
  529. result = -1
  530. leave(pkAnyRune, s, p, start, result)
  531. of pkLetter:
  532. enter(pkLetter, s, p, start)
  533. if start < s.len:
  534. var a: Rune
  535. result = start
  536. fastRuneAt(s, result, a)
  537. if isAlpha(a): dec(result, start)
  538. else: result = -1
  539. else:
  540. result = -1
  541. leave(pkLetter, s, p, start, result)
  542. of pkLower:
  543. enter(pkLower, s, p, start)
  544. if start < s.len:
  545. var a: Rune
  546. result = start
  547. fastRuneAt(s, result, a)
  548. if isLower(a): dec(result, start)
  549. else: result = -1
  550. else:
  551. result = -1
  552. leave(pkLower, s, p, start, result)
  553. of pkUpper:
  554. enter(pkUpper, s, p, start)
  555. if start < s.len:
  556. var a: Rune
  557. result = start
  558. fastRuneAt(s, result, a)
  559. if isUpper(a): dec(result, start)
  560. else: result = -1
  561. else:
  562. result = -1
  563. leave(pkUpper, s, p, start, result)
  564. of pkTitle:
  565. enter(pkTitle, s, p, start)
  566. if start < s.len:
  567. var a: Rune
  568. result = start
  569. fastRuneAt(s, result, a)
  570. if isTitle(a): dec(result, start)
  571. else: result = -1
  572. else:
  573. result = -1
  574. leave(pkTitle, s, p, start, result)
  575. of pkWhitespace:
  576. enter(pkWhitespace, s, p, start)
  577. if start < s.len:
  578. var a: Rune
  579. result = start
  580. fastRuneAt(s, result, a)
  581. if isWhiteSpace(a): dec(result, start)
  582. else: result = -1
  583. else:
  584. result = -1
  585. leave(pkWhitespace, s, p, start, result)
  586. of pkGreedyAny:
  587. enter(pkGreedyAny, s, p, start)
  588. result = len(s) - start
  589. leave(pkGreedyAny, s, p, start, result)
  590. of pkNewLine:
  591. enter(pkNewLine, s, p, start)
  592. if start < s.len and s[start] == '\L': result = 1
  593. elif start < s.len and s[start] == '\C':
  594. if start+1 < s.len and s[start+1] == '\L': result = 2
  595. else: result = 1
  596. else: result = -1
  597. leave(pkNewLine, s, p, start, result)
  598. of pkTerminal:
  599. enter(pkTerminal, s, p, start)
  600. result = len(p.term)
  601. for i in 0..result-1:
  602. if start+i >= s.len or p.term[i] != s[start+i]:
  603. result = -1
  604. break
  605. leave(pkTerminal, s, p, start, result)
  606. of pkTerminalIgnoreCase:
  607. enter(pkTerminalIgnoreCase, s, p, start)
  608. var
  609. i = 0
  610. a, b: Rune
  611. result = start
  612. while i < len(p.term):
  613. if result >= s.len:
  614. result = -1
  615. break
  616. fastRuneAt(p.term, i, a)
  617. fastRuneAt(s, result, b)
  618. if toLower(a) != toLower(b):
  619. result = -1
  620. break
  621. dec(result, start)
  622. leave(pkTerminalIgnoreCase, s, p, start, result)
  623. of pkTerminalIgnoreStyle:
  624. enter(pkTerminalIgnoreStyle, s, p, start)
  625. var
  626. i = 0
  627. a, b: Rune
  628. result = start
  629. while i < len(p.term):
  630. while i < len(p.term):
  631. fastRuneAt(p.term, i, a)
  632. if a != Rune('_'): break
  633. while result < s.len:
  634. fastRuneAt(s, result, b)
  635. if b != Rune('_'): break
  636. if result >= s.len:
  637. if i >= p.term.len: break
  638. else:
  639. result = -1
  640. break
  641. elif toLower(a) != toLower(b):
  642. result = -1
  643. break
  644. dec(result, start)
  645. leave(pkTerminalIgnoreStyle, s, p, start, result)
  646. of pkChar:
  647. enter(pkChar, s, p, start)
  648. if start < s.len and p.ch == s[start]: result = 1
  649. else: result = -1
  650. leave(pkChar, s, p, start, result)
  651. of pkCharChoice:
  652. enter(pkCharChoice, s, p, start)
  653. if start < s.len and contains(p.charChoice[], s[start]): result = 1
  654. else: result = -1
  655. leave(pkCharChoice, s, p, start, result)
  656. of pkNonTerminal:
  657. enter(pkNonTerminal, s, p, start)
  658. var oldMl = c.ml
  659. when false: echo "enter: ", p.nt.name
  660. result = mopProc(s, p.nt.rule, start, c)
  661. when false: echo "leave: ", p.nt.name
  662. if result < 0: c.ml = oldMl
  663. leave(pkNonTerminal, s, p, start, result)
  664. of pkSequence:
  665. enter(pkSequence, s, p, start)
  666. var oldMl = c.ml
  667. result = 0
  668. for i in 0..high(p.sons):
  669. var x = mopProc(s, p.sons[i], start+result, c)
  670. if x < 0:
  671. c.ml = oldMl
  672. result = -1
  673. break
  674. else: inc(result, x)
  675. leave(pkSequence, s, p, start, result)
  676. of pkOrderedChoice:
  677. enter(pkOrderedChoice, s, p, start)
  678. var oldMl = c.ml
  679. for i in 0..high(p.sons):
  680. result = mopProc(s, p.sons[i], start, c)
  681. if result >= 0: break
  682. c.ml = oldMl
  683. leave(pkOrderedChoice, s, p, start, result)
  684. of pkSearch:
  685. enter(pkSearch, s, p, start)
  686. var oldMl = c.ml
  687. result = 0
  688. while start+result <= s.len:
  689. var x = mopProc(s, p.sons[0], start+result, c)
  690. if x >= 0:
  691. inc(result, x)
  692. leave(pkSearch, s, p, start, result)
  693. return
  694. inc(result)
  695. result = -1
  696. c.ml = oldMl
  697. leave(pkSearch, s, p, start, result)
  698. of pkCapturedSearch:
  699. enter(pkCapturedSearch, s, p, start)
  700. var idx = c.ml # reserve a slot for the subpattern
  701. inc(c.ml)
  702. result = 0
  703. while start+result <= s.len:
  704. var x = mopProc(s, p.sons[0], start+result, c)
  705. if x >= 0:
  706. if idx < MaxSubpatterns:
  707. c.matches[idx] = (start, start+result-1)
  708. #else: silently ignore the capture
  709. inc(result, x)
  710. leave(pkCapturedSearch, s, p, start, result)
  711. return
  712. inc(result)
  713. result = -1
  714. c.ml = idx
  715. leave(pkCapturedSearch, s, p, start, result)
  716. of pkGreedyRep:
  717. enter(pkGreedyRep, s, p, start)
  718. result = 0
  719. while true:
  720. var x = mopProc(s, p.sons[0], start+result, c)
  721. # if x == 0, we have an endless loop; so the correct behaviour would be
  722. # not to break. But endless loops can be easily introduced:
  723. # ``(comment / \w*)*`` is such an example. Breaking for x == 0 does the
  724. # expected thing in this case.
  725. if x <= 0: break
  726. inc(result, x)
  727. leave(pkGreedyRep, s, p, start, result)
  728. of pkGreedyRepChar:
  729. enter(pkGreedyRepChar, s, p, start)
  730. result = 0
  731. var ch = p.ch
  732. while start+result < s.len and ch == s[start+result]: inc(result)
  733. leave(pkGreedyRepChar, s, p, start, result)
  734. of pkGreedyRepSet:
  735. enter(pkGreedyRepSet, s, p, start)
  736. result = 0
  737. while start+result < s.len and contains(p.charChoice[], s[start+result]): inc(result)
  738. leave(pkGreedyRepSet, s, p, start, result)
  739. of pkOption:
  740. enter(pkOption, s, p, start)
  741. result = max(0, mopProc(s, p.sons[0], start, c))
  742. leave(pkOption, s, p, start, result)
  743. of pkAndPredicate:
  744. enter(pkAndPredicate, s, p, start)
  745. var oldMl = c.ml
  746. result = mopProc(s, p.sons[0], start, c)
  747. if result >= 0: result = 0 # do not consume anything
  748. else: c.ml = oldMl
  749. leave(pkAndPredicate, s, p, start, result)
  750. of pkNotPredicate:
  751. enter(pkNotPredicate, s, p, start)
  752. var oldMl = c.ml
  753. result = mopProc(s, p.sons[0], start, c)
  754. if result < 0: result = 0
  755. else:
  756. c.ml = oldMl
  757. result = -1
  758. leave(pkNotPredicate, s, p, start, result)
  759. of pkCapture:
  760. enter(pkCapture, s, p, start)
  761. var idx = c.ml # reserve a slot for the subpattern
  762. inc(c.ml)
  763. result = mopProc(s, p.sons[0], start, c)
  764. if result >= 0:
  765. if idx < MaxSubpatterns:
  766. c.matches[idx] = (start, start+result-1)
  767. #else: silently ignore the capture
  768. else:
  769. c.ml = idx
  770. leave(pkCapture, s, p, start, result)
  771. of pkBackRef:
  772. enter(pkBackRef, s, p, start)
  773. result = matchBackRef(s, p, start, c)
  774. leave(pkBackRef, s, p, start, result)
  775. of pkBackRefIgnoreCase:
  776. enter(pkBackRefIgnoreCase, s, p, start)
  777. result = matchBackRef(s, p, start, c)
  778. leave(pkBackRefIgnoreCase, s, p, start, result)
  779. of pkBackRefIgnoreStyle:
  780. enter(pkBackRefIgnoreStyle, s, p, start)
  781. result = matchBackRef(s, p, start, c)
  782. leave(pkBackRefIgnoreStyle, s, p, start, result)
  783. of pkStartAnchor:
  784. enter(pkStartAnchor, s, p, start)
  785. if c.origStart == start: result = 0
  786. else: result = -1
  787. leave(pkStartAnchor, s, p, start, result)
  788. of pkRule, pkList: assert false
  789. proc rawMatch*(s: string, p: Peg, start: int, c: var Captures): int
  790. {.noSideEffect, rtl, extern: "npegs$1".} =
  791. ## low-level matching proc that implements the PEG interpreter. Use this
  792. ## for maximum efficiency (every other PEG operation ends up calling this
  793. ## proc).
  794. ## Returns -1 if it does not match, else the length of the match
  795. # Set the handler generators to produce do-nothing handlers.
  796. template enter(pk, s, p, start) =
  797. discard
  798. template leave(pk, s, p, start, length) =
  799. discard
  800. matchOrParse(matchIt)
  801. result = matchIt(s, p, start, c)
  802. macro mkHandlerTplts(handlers: untyped): untyped =
  803. # Transforms the handler spec in *handlers* into handler templates.
  804. # The AST structure of *handlers[0]*:
  805. #
  806. # .. code-block::
  807. # StmtList
  808. # Call
  809. # Ident "pkNonTerminal"
  810. # StmtList
  811. # Call
  812. # Ident "enter"
  813. # StmtList
  814. # <handler code block>
  815. # Call
  816. # Ident "leave"
  817. # StmtList
  818. # <handler code block>
  819. # Call
  820. # Ident "pkChar"
  821. # StmtList
  822. # Call
  823. # Ident "leave"
  824. # StmtList
  825. # <handler code block>
  826. # ...
  827. proc mkEnter(hdName, body: NimNode): NimNode =
  828. quote do:
  829. template `hdName`(s, p, start) =
  830. let s {.inject.} = s
  831. let p {.inject.} = p
  832. let start {.inject.} = start
  833. `body`
  834. template mkLeave(hdPostf, body) {.dirty.} =
  835. # this has to be dirty to be able to capture *result* as *length* in
  836. # *leaveXX* calls.
  837. template `leave hdPostf`(s, p, start, length) =
  838. body
  839. result = newStmtList()
  840. for topCall in handlers[0]:
  841. if nnkCall != topCall.kind:
  842. error("Call syntax expected.", topCall)
  843. let pegKind = topCall[0]
  844. if nnkIdent != pegKind.kind:
  845. error("PegKind expected.", pegKind)
  846. if 2 == topCall.len:
  847. for hdDef in topCall[1]:
  848. if nnkCall != hdDef.kind:
  849. error("Call syntax expected.", hdDef)
  850. if nnkIdent != hdDef[0].kind:
  851. error("Handler identifier expected.", hdDef[0])
  852. if 2 == hdDef.len:
  853. let hdPostf = substr(pegKind.strVal, 2)
  854. case hdDef[0].strVal
  855. of "enter":
  856. result.add mkEnter(newIdentNode("enter" & hdPostf), hdDef[1])
  857. of "leave":
  858. result.add getAst(mkLeave(ident(hdPostf), hdDef[1]))
  859. else:
  860. error(
  861. "Unsupported handler identifier, expected 'enter' or 'leave'.",
  862. hdDef[0]
  863. )
  864. template eventParser*(pegAst, handlers: untyped): (proc(s: string): int) =
  865. ## Generates an interpreting event parser *proc* according to the specified
  866. ## PEG AST and handler code blocks. The *proc* can be called with a string
  867. ## to be parsed and will execute the handler code blocks whenever their
  868. ## associated grammar element is matched. It returns -1 if the string does not
  869. ## match, else the length of the total match. The following example code
  870. ## evaluates an arithmetic expression defined by a simple PEG:
  871. ##
  872. ## .. code-block:: nim
  873. ## import strutils, pegs
  874. ##
  875. ## let
  876. ## pegAst = """
  877. ## Expr <- Sum
  878. ## Sum <- Product (('+' / '-')Product)*
  879. ## Product <- Value (('*' / '/')Value)*
  880. ## Value <- [0-9]+ / '(' Expr ')'
  881. ## """.peg
  882. ## txt = "(5+3)/2-7*22"
  883. ##
  884. ## var
  885. ## pStack: seq[string] = @[]
  886. ## valStack: seq[float] = @[]
  887. ## opStack = ""
  888. ## let
  889. ## parseArithExpr = pegAst.eventParser:
  890. ## pkNonTerminal:
  891. ## enter:
  892. ## pStack.add p.nt.name
  893. ## leave:
  894. ## pStack.setLen pStack.high
  895. ## if length > 0:
  896. ## let matchStr = s.substr(start, start+length-1)
  897. ## case p.nt.name
  898. ## of "Value":
  899. ## try:
  900. ## valStack.add matchStr.parseFloat
  901. ## echo valStack
  902. ## except ValueError:
  903. ## discard
  904. ## of "Sum", "Product":
  905. ## try:
  906. ## let val = matchStr.parseFloat
  907. ## except ValueError:
  908. ## if valStack.len > 1 and opStack.len > 0:
  909. ## valStack[^2] = case opStack[^1]
  910. ## of '+': valStack[^2] + valStack[^1]
  911. ## of '-': valStack[^2] - valStack[^1]
  912. ## of '*': valStack[^2] * valStack[^1]
  913. ## else: valStack[^2] / valStack[^1]
  914. ## valStack.setLen valStack.high
  915. ## echo valStack
  916. ## opStack.setLen opStack.high
  917. ## echo opStack
  918. ## pkChar:
  919. ## leave:
  920. ## if length == 1 and "Value" != pStack[^1]:
  921. ## let matchChar = s[start]
  922. ## opStack.add matchChar
  923. ## echo opStack
  924. ##
  925. ## let pLen = parseArithExpr(txt)
  926. ##
  927. ## The *handlers* parameter consists of code blocks for *PegKinds*,
  928. ## which define the grammar elements of interest. Each block can contain
  929. ## handler code to be executed when the parser enters and leaves text
  930. ## matching the grammar element. An *enter* handler can access the specific
  931. ## PEG AST node being matched as *p*, the entire parsed string as *s*
  932. ## and the position of the matched text segment in *s* as *start*. A *leave*
  933. ## handler can access *p*, *s*, *start* and also the length of the matched
  934. ## text segment as *length*. For an unsuccessful match, the *enter* and
  935. ## *leave* handlers will be executed, with *length* set to -1.
  936. ##
  937. ## Symbols declared in an *enter* handler can be made visible in the
  938. ## corresponding *leave* handler by annotating them with an *inject* pragma.
  939. proc rawParse(s: string, p: Peg, start: int, c: var Captures): int
  940. {.genSym.} =
  941. # binding from *macros*
  942. bind strVal
  943. mkHandlerTplts:
  944. handlers
  945. macro enter(pegKind, s, pegNode, start: untyped): untyped =
  946. # This is called by the matcher code in *matchOrParse* at the
  947. # start of the code for a grammar element of kind *pegKind*.
  948. # Expands to a call to the handler template if one was generated
  949. # by *mkHandlerTplts*.
  950. template mkDoEnter(hdPostf, s, pegNode, start) =
  951. when declared(`enter hdPostf`):
  952. `enter hdPostf`(s, pegNode, start):
  953. else:
  954. discard
  955. let hdPostf = ident(substr(strVal(pegKind), 2))
  956. getAst(mkDoEnter(hdPostf, s, pegNode, start))
  957. macro leave(pegKind, s, pegNode, start, length: untyped): untyped =
  958. # Like *enter*, but called at the end of the matcher code for
  959. # a grammar element of kind *pegKind*.
  960. template mkDoLeave(hdPostf, s, pegNode, start, length) =
  961. when declared(`leave hdPostf`):
  962. `leave hdPostf`(s, pegNode, start, length):
  963. else:
  964. discard
  965. let hdPostf = ident(substr(strVal(pegKind), 2))
  966. getAst(mkDoLeave(hdPostf, s, pegNode, start, length))
  967. matchOrParse(parseIt)
  968. parseIt(s, p, start, c)
  969. proc parser(s: string): int {.genSym.} =
  970. # the proc to be returned
  971. var
  972. ms: array[MaxSubpatterns, (int, int)]
  973. cs = Captures(matches: ms, ml: 0, origStart: 0)
  974. rawParse(s, pegAst, 0, cs)
  975. parser
  976. template fillMatches(s, caps, c) =
  977. for k in 0..c.ml-1:
  978. let startIdx = c.matches[k][0]
  979. let endIdx = c.matches[k][1]
  980. if startIdx != -1:
  981. caps[k] = substr(s, startIdx, endIdx)
  982. else:
  983. caps[k] = ""
  984. proc matchLen*(s: string, pattern: Peg, matches: var openArray[string],
  985. start = 0): int {.nosideEffect, rtl, extern: "npegs$1Capture".} =
  986. ## the same as ``match``, but it returns the length of the match,
  987. ## if there is no match, -1 is returned. Note that a match length
  988. ## of zero can happen. It's possible that a suffix of `s` remains
  989. ## that does not belong to the match.
  990. var c: Captures
  991. c.origStart = start
  992. result = rawMatch(s, pattern, start, c)
  993. if result >= 0: fillMatches(s, matches, c)
  994. proc matchLen*(s: string, pattern: Peg,
  995. start = 0): int {.nosideEffect, rtl, extern: "npegs$1".} =
  996. ## the same as ``match``, but it returns the length of the match,
  997. ## if there is no match, -1 is returned. Note that a match length
  998. ## of zero can happen. It's possible that a suffix of `s` remains
  999. ## that does not belong to the match.
  1000. var c: Captures
  1001. c.origStart = start
  1002. result = rawMatch(s, pattern, start, c)
  1003. proc match*(s: string, pattern: Peg, matches: var openArray[string],
  1004. start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
  1005. ## returns ``true`` if ``s[start..]`` matches the ``pattern`` and
  1006. ## the captured substrings in the array ``matches``. If it does not
  1007. ## match, nothing is written into ``matches`` and ``false`` is
  1008. ## returned.
  1009. result = matchLen(s, pattern, matches, start) != -1
  1010. proc match*(s: string, pattern: Peg,
  1011. start = 0): bool {.nosideEffect, rtl, extern: "npegs$1".} =
  1012. ## returns ``true`` if ``s`` matches the ``pattern`` beginning from ``start``.
  1013. result = matchLen(s, pattern, start) != -1
  1014. proc find*(s: string, pattern: Peg, matches: var openArray[string],
  1015. start = 0): int {.nosideEffect, rtl, extern: "npegs$1Capture".} =
  1016. ## returns the starting position of ``pattern`` in ``s`` and the captured
  1017. ## substrings in the array ``matches``. If it does not match, nothing
  1018. ## is written into ``matches`` and -1 is returned.
  1019. var c: Captures
  1020. c.origStart = start
  1021. for i in start .. s.len-1:
  1022. c.ml = 0
  1023. if rawMatch(s, pattern, i, c) >= 0:
  1024. fillMatches(s, matches, c)
  1025. return i
  1026. return -1
  1027. # could also use the pattern here: (!P .)* P
  1028. proc findBounds*(s: string, pattern: Peg, matches: var openArray[string],
  1029. start = 0): tuple[first, last: int] {.
  1030. nosideEffect, rtl, extern: "npegs$1Capture".} =
  1031. ## returns the starting position and end position of ``pattern`` in ``s``
  1032. ## and the captured
  1033. ## substrings in the array ``matches``. If it does not match, nothing
  1034. ## is written into ``matches`` and (-1,0) is returned.
  1035. var c: Captures
  1036. c.origStart = start
  1037. for i in start .. s.len-1:
  1038. c.ml = 0
  1039. var L = rawMatch(s, pattern, i, c)
  1040. if L >= 0:
  1041. fillMatches(s, matches, c)
  1042. return (i, i+L-1)
  1043. return (-1, 0)
  1044. proc find*(s: string, pattern: Peg,
  1045. start = 0): int {.nosideEffect, rtl, extern: "npegs$1".} =
  1046. ## returns the starting position of ``pattern`` in ``s``. If it does not
  1047. ## match, -1 is returned.
  1048. var c: Captures
  1049. c.origStart = start
  1050. for i in start .. s.len-1:
  1051. if rawMatch(s, pattern, i, c) >= 0: return i
  1052. return -1
  1053. iterator findAll*(s: string, pattern: Peg, start = 0): string =
  1054. ## yields all matching *substrings* of `s` that match `pattern`.
  1055. var c: Captures
  1056. c.origStart = start
  1057. var i = start
  1058. while i < s.len:
  1059. c.ml = 0
  1060. var L = rawMatch(s, pattern, i, c)
  1061. if L < 0:
  1062. inc(i, 1)
  1063. else:
  1064. yield substr(s, i, i+L-1)
  1065. inc(i, L)
  1066. proc findAll*(s: string, pattern: Peg, start = 0): seq[string] {.
  1067. nosideEffect, rtl, extern: "npegs$1".} =
  1068. ## returns all matching *substrings* of `s` that match `pattern`.
  1069. ## If it does not match, @[] is returned.
  1070. accumulateResult(findAll(s, pattern, start))
  1071. when not defined(nimhygiene):
  1072. {.pragma: inject.}
  1073. template `=~`*(s: string, pattern: Peg): bool =
  1074. ## This calls ``match`` with an implicit declared ``matches`` array that
  1075. ## can be used in the scope of the ``=~`` call:
  1076. ##
  1077. ## .. code-block:: nim
  1078. ##
  1079. ## if line =~ peg"\s* {\w+} \s* '=' \s* {\w+}":
  1080. ## # matches a key=value pair:
  1081. ## echo("Key: ", matches[0])
  1082. ## echo("Value: ", matches[1])
  1083. ## elif line =~ peg"\s*{'#'.*}":
  1084. ## # matches a comment
  1085. ## # note that the implicit ``matches`` array is different from the
  1086. ## # ``matches`` array of the first branch
  1087. ## echo("comment: ", matches[0])
  1088. ## else:
  1089. ## echo("syntax error")
  1090. ##
  1091. bind MaxSubpatterns
  1092. when not declaredInScope(matches):
  1093. var matches {.inject.}: array[0..MaxSubpatterns-1, string]
  1094. match(s, pattern, matches)
  1095. # ------------------------- more string handling ------------------------------
  1096. proc contains*(s: string, pattern: Peg, start = 0): bool {.
  1097. nosideEffect, rtl, extern: "npegs$1".} =
  1098. ## same as ``find(s, pattern, start) >= 0``
  1099. return find(s, pattern, start) >= 0
  1100. proc contains*(s: string, pattern: Peg, matches: var openArray[string],
  1101. start = 0): bool {.nosideEffect, rtl, extern: "npegs$1Capture".} =
  1102. ## same as ``find(s, pattern, matches, start) >= 0``
  1103. return find(s, pattern, matches, start) >= 0
  1104. proc startsWith*(s: string, prefix: Peg, start = 0): bool {.
  1105. nosideEffect, rtl, extern: "npegs$1".} =
  1106. ## returns true if `s` starts with the pattern `prefix`
  1107. result = matchLen(s, prefix, start) >= 0
  1108. proc endsWith*(s: string, suffix: Peg, start = 0): bool {.
  1109. nosideEffect, rtl, extern: "npegs$1".} =
  1110. ## returns true if `s` ends with the pattern `suffix`
  1111. var c: Captures
  1112. c.origStart = start
  1113. for i in start .. s.len-1:
  1114. if rawMatch(s, suffix, i, c) == s.len - i: return true
  1115. proc replacef*(s: string, sub: Peg, by: string): string {.
  1116. nosideEffect, rtl, extern: "npegs$1".} =
  1117. ## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by`
  1118. ## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples:
  1119. ##
  1120. ## .. code-block:: nim
  1121. ## "var1=key; var2=key2".replacef(peg"{\ident}'='{\ident}", "$1<-$2$2")
  1122. ##
  1123. ## Results in:
  1124. ##
  1125. ## .. code-block:: nim
  1126. ##
  1127. ## "var1<-keykey; val2<-key2key2"
  1128. result = ""
  1129. var i = 0
  1130. var caps: array[0..MaxSubpatterns-1, string]
  1131. var c: Captures
  1132. while i < s.len:
  1133. c.ml = 0
  1134. var x = rawMatch(s, sub, i, c)
  1135. if x <= 0:
  1136. add(result, s[i])
  1137. inc(i)
  1138. else:
  1139. fillMatches(s, caps, c)
  1140. addf(result, by, caps)
  1141. inc(i, x)
  1142. add(result, substr(s, i))
  1143. proc replace*(s: string, sub: Peg, by = ""): string {.
  1144. nosideEffect, rtl, extern: "npegs$1".} =
  1145. ## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed
  1146. ## in `by`.
  1147. result = ""
  1148. var i = 0
  1149. var c: Captures
  1150. while i < s.len:
  1151. var x = rawMatch(s, sub, i, c)
  1152. if x <= 0:
  1153. add(result, s[i])
  1154. inc(i)
  1155. else:
  1156. add(result, by)
  1157. inc(i, x)
  1158. add(result, substr(s, i))
  1159. proc parallelReplace*(s: string, subs: varargs[
  1160. tuple[pattern: Peg, repl: string]]): string {.
  1161. nosideEffect, rtl, extern: "npegs$1".} =
  1162. ## Returns a modified copy of `s` with the substitutions in `subs`
  1163. ## applied in parallel.
  1164. result = ""
  1165. var i = 0
  1166. var c: Captures
  1167. var caps: array[0..MaxSubpatterns-1, string]
  1168. while i < s.len:
  1169. block searchSubs:
  1170. for j in 0..high(subs):
  1171. c.ml = 0
  1172. var x = rawMatch(s, subs[j][0], i, c)
  1173. if x > 0:
  1174. fillMatches(s, caps, c)
  1175. addf(result, subs[j][1], caps)
  1176. inc(i, x)
  1177. break searchSubs
  1178. add(result, s[i])
  1179. inc(i)
  1180. # copy the rest:
  1181. add(result, substr(s, i))
  1182. proc replace*(s: string, sub: Peg, cb: proc(
  1183. match: int, cnt: int, caps: openArray[string]): string): string {.
  1184. rtl, extern: "npegs$1cb".}=
  1185. ## Replaces `sub` in `s` by the resulting strings from the callback.
  1186. ## The callback proc receives the index of the current match (starting with 0),
  1187. ## the count of captures and an open array with the captures of each match. Examples:
  1188. ##
  1189. ## .. code-block:: nim
  1190. ##
  1191. ## proc handleMatches*(m: int, n: int, c: openArray[string]): string =
  1192. ## result = ""
  1193. ## if m > 0:
  1194. ## result.add ", "
  1195. ## result.add case n:
  1196. ## of 2: c[0].toLower & ": '" & c[1] & "'"
  1197. ## of 1: c[0].toLower & ": ''"
  1198. ## else: ""
  1199. ##
  1200. ## let s = "Var1=key1;var2=Key2; VAR3"
  1201. ## echo s.replace(peg"{\ident}('='{\ident})* ';'* \s*", handleMatches)
  1202. ##
  1203. ## Results in:
  1204. ##
  1205. ## .. code-block:: nim
  1206. ##
  1207. ## "var1: 'key1', var2: 'Key2', var3: ''"
  1208. result = ""
  1209. var i = 0
  1210. var caps: array[0..MaxSubpatterns-1, string]
  1211. var c: Captures
  1212. var m = 0
  1213. while i < s.len:
  1214. c.ml = 0
  1215. var x = rawMatch(s, sub, i, c)
  1216. if x <= 0:
  1217. add(result, s[i])
  1218. inc(i)
  1219. else:
  1220. fillMatches(s, caps, c)
  1221. add(result, cb(m, c.ml, caps))
  1222. inc(i, x)
  1223. inc(m)
  1224. add(result, substr(s, i))
  1225. when not defined(js):
  1226. proc transformFile*(infile, outfile: string,
  1227. subs: varargs[tuple[pattern: Peg, repl: string]]) {.
  1228. rtl, extern: "npegs$1".} =
  1229. ## reads in the file `infile`, performs a parallel replacement (calls
  1230. ## `parallelReplace`) and writes back to `outfile`. Raises ``IOError`` if an
  1231. ## error occurs. This is supposed to be used for quick scripting.
  1232. ##
  1233. ## **Note**: this proc does not exist while using the JS backend.
  1234. var x = readFile(infile).string
  1235. writeFile(outfile, x.parallelReplace(subs))
  1236. iterator split*(s: string, sep: Peg): string =
  1237. ## Splits the string `s` into substrings.
  1238. ##
  1239. ## Substrings are separated by the PEG `sep`.
  1240. ## Examples:
  1241. ##
  1242. ## .. code-block:: nim
  1243. ## for word in split("00232this02939is39an22example111", peg"\d+"):
  1244. ## writeLine(stdout, word)
  1245. ##
  1246. ## Results in:
  1247. ##
  1248. ## .. code-block:: nim
  1249. ## "this"
  1250. ## "is"
  1251. ## "an"
  1252. ## "example"
  1253. ##
  1254. var c: Captures
  1255. var
  1256. first = 0
  1257. last = 0
  1258. while last < len(s):
  1259. c.ml = 0
  1260. var x = rawMatch(s, sep, last, c)
  1261. if x > 0: inc(last, x)
  1262. first = last
  1263. while last < len(s):
  1264. inc(last)
  1265. c.ml = 0
  1266. x = rawMatch(s, sep, last, c)
  1267. if x > 0: break
  1268. if first < last:
  1269. yield substr(s, first, last-1)
  1270. proc split*(s: string, sep: Peg): seq[string] {.
  1271. nosideEffect, rtl, extern: "npegs$1".} =
  1272. ## Splits the string `s` into substrings.
  1273. accumulateResult(split(s, sep))
  1274. # ------------------- scanner -------------------------------------------------
  1275. type
  1276. Modifier = enum
  1277. modNone,
  1278. modVerbatim,
  1279. modIgnoreCase,
  1280. modIgnoreStyle
  1281. TokKind = enum ## enumeration of all tokens
  1282. tkInvalid, ## invalid token
  1283. tkEof, ## end of file reached
  1284. tkAny, ## .
  1285. tkAnyRune, ## _
  1286. tkIdentifier, ## abc
  1287. tkStringLit, ## "abc" or 'abc'
  1288. tkCharSet, ## [^A-Z]
  1289. tkParLe, ## '('
  1290. tkParRi, ## ')'
  1291. tkCurlyLe, ## '{'
  1292. tkCurlyRi, ## '}'
  1293. tkCurlyAt, ## '{@}'
  1294. tkArrow, ## '<-'
  1295. tkBar, ## '/'
  1296. tkStar, ## '*'
  1297. tkPlus, ## '+'
  1298. tkAmp, ## '&'
  1299. tkNot, ## '!'
  1300. tkOption, ## '?'
  1301. tkAt, ## '@'
  1302. tkBuiltin, ## \identifier
  1303. tkEscaped, ## \\
  1304. tkBackref, ## '$'
  1305. tkDollar, ## '$'
  1306. tkHat ## '^'
  1307. Token {.final.} = object ## a token
  1308. kind: TokKind ## the type of the token
  1309. modifier: Modifier
  1310. literal: string ## the parsed (string) literal
  1311. charset: set[char] ## if kind == tkCharSet
  1312. index: int ## if kind == tkBackref
  1313. PegLexer {.inheritable.} = object ## the lexer object.
  1314. bufpos: int ## the current position within the buffer
  1315. buf: cstring ## the buffer itself
  1316. lineNumber: int ## the current line number
  1317. lineStart: int ## index of last line start in buffer
  1318. colOffset: int ## column to add
  1319. filename: string
  1320. const
  1321. tokKindToStr: array[TokKind, string] = [
  1322. "invalid", "[EOF]", ".", "_", "identifier", "string literal",
  1323. "character set", "(", ")", "{", "}", "{@}",
  1324. "<-", "/", "*", "+", "&", "!", "?",
  1325. "@", "built-in", "escaped", "$", "$", "^"
  1326. ]
  1327. proc handleCR(L: var PegLexer, pos: int): int =
  1328. assert(L.buf[pos] == '\c')
  1329. inc(L.lineNumber)
  1330. result = pos+1
  1331. if result < L.buf.len and L.buf[result] == '\L': inc(result)
  1332. L.lineStart = result
  1333. proc handleLF(L: var PegLexer, pos: int): int =
  1334. assert(L.buf[pos] == '\L')
  1335. inc(L.lineNumber)
  1336. result = pos+1
  1337. L.lineStart = result
  1338. proc init(L: var PegLexer, input, filename: string, line = 1, col = 0) =
  1339. L.buf = input
  1340. L.bufpos = 0
  1341. L.lineNumber = line
  1342. L.colOffset = col
  1343. L.lineStart = 0
  1344. L.filename = filename
  1345. proc getColumn(L: PegLexer): int {.inline.} =
  1346. result = abs(L.bufpos - L.lineStart) + L.colOffset
  1347. proc getLine(L: PegLexer): int {.inline.} =
  1348. result = L.lineNumber
  1349. proc errorStr(L: PegLexer, msg: string, line = -1, col = -1): string =
  1350. var line = if line < 0: getLine(L) else: line
  1351. var col = if col < 0: getColumn(L) else: col
  1352. result = "$1($2, $3) Error: $4" % [L.filename, $line, $col, msg]
  1353. proc handleHexChar(c: var PegLexer, xi: var int) =
  1354. case c.buf[c.bufpos]
  1355. of '0'..'9':
  1356. xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('0'))
  1357. inc(c.bufpos)
  1358. of 'a'..'f':
  1359. xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('a') + 10)
  1360. inc(c.bufpos)
  1361. of 'A'..'F':
  1362. xi = (xi shl 4) or (ord(c.buf[c.bufpos]) - ord('A') + 10)
  1363. inc(c.bufpos)
  1364. else: discard
  1365. proc getEscapedChar(c: var PegLexer, tok: var Token) =
  1366. inc(c.bufpos)
  1367. case c.buf[c.bufpos]
  1368. of 'r', 'R', 'c', 'C':
  1369. add(tok.literal, '\c')
  1370. inc(c.bufpos)
  1371. of 'l', 'L':
  1372. add(tok.literal, '\L')
  1373. inc(c.bufpos)
  1374. of 'f', 'F':
  1375. add(tok.literal, '\f')
  1376. inc(c.bufpos)
  1377. of 'e', 'E':
  1378. add(tok.literal, '\e')
  1379. inc(c.bufpos)
  1380. of 'a', 'A':
  1381. add(tok.literal, '\a')
  1382. inc(c.bufpos)
  1383. of 'b', 'B':
  1384. add(tok.literal, '\b')
  1385. inc(c.bufpos)
  1386. of 'v', 'V':
  1387. add(tok.literal, '\v')
  1388. inc(c.bufpos)
  1389. of 't', 'T':
  1390. add(tok.literal, '\t')
  1391. inc(c.bufpos)
  1392. of 'x', 'X':
  1393. inc(c.bufpos)
  1394. var xi = 0
  1395. handleHexChar(c, xi)
  1396. handleHexChar(c, xi)
  1397. if xi == 0: tok.kind = tkInvalid
  1398. else: add(tok.literal, chr(xi))
  1399. of '0'..'9':
  1400. var val = ord(c.buf[c.bufpos]) - ord('0')
  1401. inc(c.bufpos)
  1402. var i = 1
  1403. while (i <= 3) and (c.buf[c.bufpos] in {'0'..'9'}):
  1404. val = val * 10 + ord(c.buf[c.bufpos]) - ord('0')
  1405. inc(c.bufpos)
  1406. inc(i)
  1407. if val > 0 and val <= 255: add(tok.literal, chr(val))
  1408. else: tok.kind = tkInvalid
  1409. of '\0'..'\31':
  1410. tok.kind = tkInvalid
  1411. elif c.buf[c.bufpos] in strutils.Letters:
  1412. tok.kind = tkInvalid
  1413. else:
  1414. add(tok.literal, c.buf[c.bufpos])
  1415. inc(c.bufpos)
  1416. proc skip(c: var PegLexer) =
  1417. var pos = c.bufpos
  1418. var buf = c.buf
  1419. while pos < c.buf.len:
  1420. case buf[pos]
  1421. of ' ', '\t':
  1422. inc(pos)
  1423. of '#':
  1424. while (pos < c.buf.len) and
  1425. not (buf[pos] in {'\c', '\L', '\0'}): inc(pos)
  1426. of '\c':
  1427. pos = handleCR(c, pos)
  1428. buf = c.buf
  1429. of '\L':
  1430. pos = handleLF(c, pos)
  1431. buf = c.buf
  1432. else:
  1433. break # EndOfFile also leaves the loop
  1434. c.bufpos = pos
  1435. proc getString(c: var PegLexer, tok: var Token) =
  1436. tok.kind = tkStringLit
  1437. var pos = c.bufpos + 1
  1438. var buf = c.buf
  1439. var quote = buf[pos-1]
  1440. while pos < c.buf.len:
  1441. case buf[pos]
  1442. of '\\':
  1443. c.bufpos = pos
  1444. getEscapedChar(c, tok)
  1445. pos = c.bufpos
  1446. of '\c', '\L', '\0':
  1447. tok.kind = tkInvalid
  1448. break
  1449. elif buf[pos] == quote:
  1450. inc(pos)
  1451. break
  1452. else:
  1453. add(tok.literal, buf[pos])
  1454. inc(pos)
  1455. c.bufpos = pos
  1456. proc getDollar(c: var PegLexer, tok: var Token) =
  1457. var pos = c.bufpos + 1
  1458. var buf = c.buf
  1459. if buf[pos] in {'0'..'9'}:
  1460. tok.kind = tkBackref
  1461. tok.index = 0
  1462. while pos < c.buf.len and buf[pos] in {'0'..'9'}:
  1463. tok.index = tok.index * 10 + ord(buf[pos]) - ord('0')
  1464. inc(pos)
  1465. else:
  1466. tok.kind = tkDollar
  1467. c.bufpos = pos
  1468. proc getCharSet(c: var PegLexer, tok: var Token) =
  1469. tok.kind = tkCharSet
  1470. tok.charset = {}
  1471. var pos = c.bufpos + 1
  1472. var buf = c.buf
  1473. var caret = false
  1474. if buf[pos] == '^':
  1475. inc(pos)
  1476. caret = true
  1477. while pos < c.buf.len:
  1478. var ch: char
  1479. case buf[pos]
  1480. of ']':
  1481. if pos < c.buf.len: inc(pos)
  1482. break
  1483. of '\\':
  1484. c.bufpos = pos
  1485. getEscapedChar(c, tok)
  1486. pos = c.bufpos
  1487. ch = tok.literal[tok.literal.len-1]
  1488. of '\C', '\L', '\0':
  1489. tok.kind = tkInvalid
  1490. break
  1491. else:
  1492. ch = buf[pos]
  1493. inc(pos)
  1494. incl(tok.charset, ch)
  1495. if buf[pos] == '-':
  1496. if pos+1 < c.buf.len and buf[pos+1] == ']':
  1497. incl(tok.charset, '-')
  1498. inc(pos)
  1499. else:
  1500. if pos+1 < c.buf.len:
  1501. inc(pos)
  1502. else:
  1503. break
  1504. var ch2: char
  1505. case buf[pos]
  1506. of '\\':
  1507. c.bufpos = pos
  1508. getEscapedChar(c, tok)
  1509. pos = c.bufpos
  1510. ch2 = tok.literal[tok.literal.len-1]
  1511. of '\C', '\L', '\0':
  1512. tok.kind = tkInvalid
  1513. break
  1514. else:
  1515. if pos+1 < c.buf.len:
  1516. ch2 = buf[pos]
  1517. inc(pos)
  1518. else:
  1519. break
  1520. for i in ord(ch)+1 .. ord(ch2):
  1521. incl(tok.charset, chr(i))
  1522. c.bufpos = pos
  1523. if caret: tok.charset = {'\1'..'\xFF'} - tok.charset
  1524. proc getSymbol(c: var PegLexer, tok: var Token) =
  1525. var pos = c.bufpos
  1526. var buf = c.buf
  1527. while pos < c.buf.len:
  1528. add(tok.literal, buf[pos])
  1529. inc(pos)
  1530. if pos < buf.len and buf[pos] notin strutils.IdentChars: break
  1531. c.bufpos = pos
  1532. tok.kind = tkIdentifier
  1533. proc getBuiltin(c: var PegLexer, tok: var Token) =
  1534. if c.bufpos+1 < c.buf.len and c.buf[c.bufpos+1] in strutils.Letters:
  1535. inc(c.bufpos)
  1536. getSymbol(c, tok)
  1537. tok.kind = tkBuiltin
  1538. else:
  1539. tok.kind = tkEscaped
  1540. getEscapedChar(c, tok) # may set tok.kind to tkInvalid
  1541. proc getTok(c: var PegLexer, tok: var Token) =
  1542. tok.kind = tkInvalid
  1543. tok.modifier = modNone
  1544. setLen(tok.literal, 0)
  1545. skip(c)
  1546. case c.buf[c.bufpos]
  1547. of '{':
  1548. inc(c.bufpos)
  1549. if c.buf[c.bufpos] == '@' and c.bufpos+2 < c.buf.len and
  1550. c.buf[c.bufpos+1] == '}':
  1551. tok.kind = tkCurlyAt
  1552. inc(c.bufpos, 2)
  1553. add(tok.literal, "{@}")
  1554. else:
  1555. tok.kind = tkCurlyLe
  1556. add(tok.literal, '{')
  1557. of '}':
  1558. tok.kind = tkCurlyRi
  1559. inc(c.bufpos)
  1560. add(tok.literal, '}')
  1561. of '[':
  1562. getCharSet(c, tok)
  1563. of '(':
  1564. tok.kind = tkParLe
  1565. inc(c.bufpos)
  1566. add(tok.literal, '(')
  1567. of ')':
  1568. tok.kind = tkParRi
  1569. inc(c.bufpos)
  1570. add(tok.literal, ')')
  1571. of '.':
  1572. tok.kind = tkAny
  1573. inc(c.bufpos)
  1574. add(tok.literal, '.')
  1575. of '_':
  1576. tok.kind = tkAnyRune
  1577. inc(c.bufpos)
  1578. add(tok.literal, '_')
  1579. of '\\':
  1580. getBuiltin(c, tok)
  1581. of '\'', '"': getString(c, tok)
  1582. of '$': getDollar(c, tok)
  1583. of 'a'..'z', 'A'..'Z', '\128'..'\255':
  1584. getSymbol(c, tok)
  1585. if c.buf[c.bufpos] in {'\'', '"'} or
  1586. c.buf[c.bufpos] == '$' and c.bufpos+1 < c.buf.len and
  1587. c.buf[c.bufpos+1] in {'0'..'9'}:
  1588. case tok.literal
  1589. of "i": tok.modifier = modIgnoreCase
  1590. of "y": tok.modifier = modIgnoreStyle
  1591. of "v": tok.modifier = modVerbatim
  1592. else: discard
  1593. setLen(tok.literal, 0)
  1594. if c.buf[c.bufpos] == '$':
  1595. getDollar(c, tok)
  1596. else:
  1597. getString(c, tok)
  1598. if tok.modifier == modNone: tok.kind = tkInvalid
  1599. of '+':
  1600. tok.kind = tkPlus
  1601. inc(c.bufpos)
  1602. add(tok.literal, '+')
  1603. of '*':
  1604. tok.kind = tkStar
  1605. inc(c.bufpos)
  1606. add(tok.literal, '+')
  1607. of '<':
  1608. if c.bufpos+2 < c.buf.len and c.buf[c.bufpos+1] == '-':
  1609. inc(c.bufpos, 2)
  1610. tok.kind = tkArrow
  1611. add(tok.literal, "<-")
  1612. else:
  1613. add(tok.literal, '<')
  1614. of '/':
  1615. tok.kind = tkBar
  1616. inc(c.bufpos)
  1617. add(tok.literal, '/')
  1618. of '?':
  1619. tok.kind = tkOption
  1620. inc(c.bufpos)
  1621. add(tok.literal, '?')
  1622. of '!':
  1623. tok.kind = tkNot
  1624. inc(c.bufpos)
  1625. add(tok.literal, '!')
  1626. of '&':
  1627. tok.kind = tkAmp
  1628. inc(c.bufpos)
  1629. add(tok.literal, '!')
  1630. of '@':
  1631. tok.kind = tkAt
  1632. inc(c.bufpos)
  1633. add(tok.literal, '@')
  1634. if c.buf[c.bufpos] == '@':
  1635. tok.kind = tkCurlyAt
  1636. inc(c.bufpos)
  1637. add(tok.literal, '@')
  1638. of '^':
  1639. tok.kind = tkHat
  1640. inc(c.bufpos)
  1641. add(tok.literal, '^')
  1642. else:
  1643. if c.bufpos >= c.buf.len:
  1644. tok.kind = tkEof
  1645. tok.literal = "[EOF]"
  1646. add(tok.literal, c.buf[c.bufpos])
  1647. inc(c.bufpos)
  1648. proc arrowIsNextTok(c: PegLexer): bool =
  1649. # the only look ahead we need
  1650. var pos = c.bufpos
  1651. while pos < c.buf.len and c.buf[pos] in {'\t', ' '}: inc(pos)
  1652. result = c.buf[pos] == '<' and (pos+1 < c.buf.len) and c.buf[pos+1] == '-'
  1653. # ----------------------------- parser ----------------------------------------
  1654. type
  1655. EInvalidPeg* = object of ValueError ## raised if an invalid
  1656. ## PEG has been detected
  1657. PegParser = object of PegLexer ## the PEG parser object
  1658. tok: Token
  1659. nonterms: seq[NonTerminal]
  1660. modifier: Modifier
  1661. captures: int
  1662. identIsVerbatim: bool
  1663. skip: Peg
  1664. proc pegError(p: PegParser, msg: string, line = -1, col = -1) =
  1665. var e: ref EInvalidPeg
  1666. new(e)
  1667. e.msg = errorStr(p, msg, line, col)
  1668. raise e
  1669. proc getTok(p: var PegParser) =
  1670. getTok(p, p.tok)
  1671. if p.tok.kind == tkInvalid: pegError(p, "'" & p.tok.literal & "' is invalid token")
  1672. proc eat(p: var PegParser, kind: TokKind) =
  1673. if p.tok.kind == kind: getTok(p)
  1674. else: pegError(p, tokKindToStr[kind] & " expected")
  1675. proc parseExpr(p: var PegParser): Peg {.gcsafe.}
  1676. proc getNonTerminal(p: var PegParser, name: string): NonTerminal =
  1677. for i in 0..high(p.nonterms):
  1678. result = p.nonterms[i]
  1679. if cmpIgnoreStyle(result.name, name) == 0: return
  1680. # forward reference:
  1681. result = newNonTerminal(name, getLine(p), getColumn(p))
  1682. add(p.nonterms, result)
  1683. proc modifiedTerm(s: string, m: Modifier): Peg =
  1684. case m
  1685. of modNone, modVerbatim: result = term(s)
  1686. of modIgnoreCase: result = termIgnoreCase(s)
  1687. of modIgnoreStyle: result = termIgnoreStyle(s)
  1688. proc modifiedBackref(s: int, m: Modifier): Peg =
  1689. case m
  1690. of modNone, modVerbatim: result = backref(s)
  1691. of modIgnoreCase: result = backrefIgnoreCase(s)
  1692. of modIgnoreStyle: result = backrefIgnoreStyle(s)
  1693. proc builtin(p: var PegParser): Peg =
  1694. # do not use "y", "skip" or "i" as these would be ambiguous
  1695. case p.tok.literal
  1696. of "n": result = newLine()
  1697. of "d": result = charSet({'0'..'9'})
  1698. of "D": result = charSet({'\1'..'\xff'} - {'0'..'9'})
  1699. of "s": result = charSet({' ', '\9'..'\13'})
  1700. of "S": result = charSet({'\1'..'\xff'} - {' ', '\9'..'\13'})
  1701. of "w": result = charSet({'a'..'z', 'A'..'Z', '_', '0'..'9'})
  1702. of "W": result = charSet({'\1'..'\xff'} - {'a'..'z','A'..'Z','_','0'..'9'})
  1703. of "a": result = charSet({'a'..'z', 'A'..'Z'})
  1704. of "A": result = charSet({'\1'..'\xff'} - {'a'..'z', 'A'..'Z'})
  1705. of "ident": result = pegs.ident
  1706. of "letter": result = unicodeLetter()
  1707. of "upper": result = unicodeUpper()
  1708. of "lower": result = unicodeLower()
  1709. of "title": result = unicodeTitle()
  1710. of "white": result = unicodeWhitespace()
  1711. else: pegError(p, "unknown built-in: " & p.tok.literal)
  1712. proc token(terminal: Peg, p: PegParser): Peg =
  1713. if p.skip.kind == pkEmpty: result = terminal
  1714. else: result = sequence(p.skip, terminal)
  1715. proc primary(p: var PegParser): Peg =
  1716. case p.tok.kind
  1717. of tkAmp:
  1718. getTok(p)
  1719. return &primary(p)
  1720. of tkNot:
  1721. getTok(p)
  1722. return !primary(p)
  1723. of tkAt:
  1724. getTok(p)
  1725. return !*primary(p)
  1726. of tkCurlyAt:
  1727. getTok(p)
  1728. return !*\primary(p).token(p)
  1729. else: discard
  1730. case p.tok.kind
  1731. of tkIdentifier:
  1732. if p.identIsVerbatim:
  1733. var m = p.tok.modifier
  1734. if m == modNone: m = p.modifier
  1735. result = modifiedTerm(p.tok.literal, m).token(p)
  1736. getTok(p)
  1737. elif not arrowIsNextTok(p):
  1738. var nt = getNonTerminal(p, p.tok.literal)
  1739. incl(nt.flags, ntUsed)
  1740. result = nonterminal(nt).token(p)
  1741. getTok(p)
  1742. else:
  1743. pegError(p, "expression expected, but found: " & p.tok.literal)
  1744. of tkStringLit:
  1745. var m = p.tok.modifier
  1746. if m == modNone: m = p.modifier
  1747. result = modifiedTerm(p.tok.literal, m).token(p)
  1748. getTok(p)
  1749. of tkCharSet:
  1750. if '\0' in p.tok.charset:
  1751. pegError(p, "binary zero ('\\0') not allowed in character class")
  1752. result = charSet(p.tok.charset).token(p)
  1753. getTok(p)
  1754. of tkParLe:
  1755. getTok(p)
  1756. result = parseExpr(p)
  1757. eat(p, tkParRi)
  1758. of tkCurlyLe:
  1759. getTok(p)
  1760. result = capture(parseExpr(p)).token(p)
  1761. eat(p, tkCurlyRi)
  1762. inc(p.captures)
  1763. of tkAny:
  1764. result = any().token(p)
  1765. getTok(p)
  1766. of tkAnyRune:
  1767. result = anyRune().token(p)
  1768. getTok(p)
  1769. of tkBuiltin:
  1770. result = builtin(p).token(p)
  1771. getTok(p)
  1772. of tkEscaped:
  1773. result = term(p.tok.literal[0]).token(p)
  1774. getTok(p)
  1775. of tkDollar:
  1776. result = endAnchor()
  1777. getTok(p)
  1778. of tkHat:
  1779. result = startAnchor()
  1780. getTok(p)
  1781. of tkBackref:
  1782. var m = p.tok.modifier
  1783. if m == modNone: m = p.modifier
  1784. result = modifiedBackref(p.tok.index, m).token(p)
  1785. if p.tok.index < 0 or p.tok.index > p.captures:
  1786. pegError(p, "invalid back reference index: " & $p.tok.index)
  1787. getTok(p)
  1788. else:
  1789. pegError(p, "expression expected, but found: " & p.tok.literal)
  1790. getTok(p) # we must consume a token here to prevent endless loops!
  1791. while true:
  1792. case p.tok.kind
  1793. of tkOption:
  1794. result = ?result
  1795. getTok(p)
  1796. of tkStar:
  1797. result = *result
  1798. getTok(p)
  1799. of tkPlus:
  1800. result = +result
  1801. getTok(p)
  1802. else: break
  1803. proc seqExpr(p: var PegParser): Peg =
  1804. result = primary(p)
  1805. while true:
  1806. case p.tok.kind
  1807. of tkAmp, tkNot, tkAt, tkStringLit, tkCharSet, tkParLe, tkCurlyLe,
  1808. tkAny, tkAnyRune, tkBuiltin, tkEscaped, tkDollar, tkBackref,
  1809. tkHat, tkCurlyAt:
  1810. result = sequence(result, primary(p))
  1811. of tkIdentifier:
  1812. if not arrowIsNextTok(p):
  1813. result = sequence(result, primary(p))
  1814. else: break
  1815. else: break
  1816. proc parseExpr(p: var PegParser): Peg =
  1817. result = seqExpr(p)
  1818. while p.tok.kind == tkBar:
  1819. getTok(p)
  1820. result = result / seqExpr(p)
  1821. proc parseRule(p: var PegParser): NonTerminal =
  1822. if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
  1823. result = getNonTerminal(p, p.tok.literal)
  1824. if ntDeclared in result.flags:
  1825. pegError(p, "attempt to redefine: " & result.name)
  1826. result.line = getLine(p)
  1827. result.col = getColumn(p)
  1828. getTok(p)
  1829. eat(p, tkArrow)
  1830. result.rule = parseExpr(p)
  1831. incl(result.flags, ntDeclared) # NOW inlining may be attempted
  1832. else:
  1833. pegError(p, "rule expected, but found: " & p.tok.literal)
  1834. proc rawParse(p: var PegParser): Peg =
  1835. ## parses a rule or a PEG expression
  1836. while p.tok.kind == tkBuiltin:
  1837. case p.tok.literal
  1838. of "i":
  1839. p.modifier = modIgnoreCase
  1840. getTok(p)
  1841. of "y":
  1842. p.modifier = modIgnoreStyle
  1843. getTok(p)
  1844. of "skip":
  1845. getTok(p)
  1846. p.skip = ?primary(p)
  1847. else: break
  1848. if p.tok.kind == tkIdentifier and arrowIsNextTok(p):
  1849. result = parseRule(p).rule
  1850. while p.tok.kind != tkEof:
  1851. discard parseRule(p)
  1852. else:
  1853. p.identIsVerbatim = true
  1854. result = parseExpr(p)
  1855. if p.tok.kind != tkEof:
  1856. pegError(p, "EOF expected, but found: " & p.tok.literal)
  1857. for i in 0..high(p.nonterms):
  1858. var nt = p.nonterms[i]
  1859. if ntDeclared notin nt.flags:
  1860. pegError(p, "undeclared identifier: " & nt.name, nt.line, nt.col)
  1861. elif ntUsed notin nt.flags and i > 0:
  1862. pegError(p, "unused rule: " & nt.name, nt.line, nt.col)
  1863. proc parsePeg*(pattern: string, filename = "pattern", line = 1, col = 0): Peg =
  1864. ## constructs a Peg object from `pattern`. `filename`, `line`, `col` are
  1865. ## used for error messages, but they only provide start offsets. `parsePeg`
  1866. ## keeps track of line and column numbers within `pattern`.
  1867. var p: PegParser
  1868. init(PegLexer(p), pattern, filename, line, col)
  1869. p.tok.kind = tkInvalid
  1870. p.tok.modifier = modNone
  1871. p.tok.literal = ""
  1872. p.tok.charset = {}
  1873. p.nonterms = @[]
  1874. p.identIsVerbatim = false
  1875. getTok(p)
  1876. result = rawParse(p)
  1877. proc peg*(pattern: string): Peg =
  1878. ## constructs a Peg object from the `pattern`. The short name has been
  1879. ## chosen to encourage its use as a raw string modifier::
  1880. ##
  1881. ## peg"{\ident} \s* '=' \s* {.*}"
  1882. result = parsePeg(pattern, "pattern")
  1883. proc escapePeg*(s: string): string =
  1884. ## escapes `s` so that it is matched verbatim when used as a peg.
  1885. result = ""
  1886. var inQuote = false
  1887. for c in items(s):
  1888. case c
  1889. of '\0'..'\31', '\'', '"', '\\':
  1890. if inQuote:
  1891. result.add('\'')
  1892. inQuote = false
  1893. result.add("\\x")
  1894. result.add(toHex(ord(c), 2))
  1895. else:
  1896. if not inQuote:
  1897. result.add('\'')
  1898. inQuote = true
  1899. result.add(c)
  1900. if inQuote: result.add('\'')
  1901. when isMainModule:
  1902. assert escapePeg("abc''def'") == r"'abc'\x27\x27'def'\x27"
  1903. assert match("(a b c)", peg"'(' @ ')'")
  1904. assert match("W_HI_Le", peg"\y 'while'")
  1905. assert(not match("W_HI_L", peg"\y 'while'"))
  1906. assert(not match("W_HI_Le", peg"\y v'while'"))
  1907. assert match("W_HI_Le", peg"y'while'")
  1908. assert($ +digits == $peg"\d+")
  1909. assert "0158787".match(peg"\d+")
  1910. assert "ABC 0232".match(peg"\w+\s+\d+")
  1911. assert "ABC".match(peg"\d+ / \w+")
  1912. var accum: seq[string] = @[]
  1913. for word in split("00232this02939is39an22example111", peg"\d+"):
  1914. accum.add(word)
  1915. assert(accum == @["this", "is", "an", "example"])
  1916. assert matchLen("key", ident) == 3
  1917. var pattern = sequence(ident, *whitespace, term('='), *whitespace, ident)
  1918. assert matchLen("key1= cal9", pattern) == 11
  1919. var ws = newNonTerminal("ws", 1, 1)
  1920. ws.rule = *whitespace
  1921. var expr = newNonTerminal("expr", 1, 1)
  1922. expr.rule = sequence(capture(ident), *sequence(
  1923. nonterminal(ws), term('+'), nonterminal(ws), nonterminal(expr)))
  1924. var c: Captures
  1925. var s = "a+b + c +d+e+f"
  1926. assert rawMatch(s, expr.rule, 0, c) == len(s)
  1927. var a = ""
  1928. for i in 0..c.ml-1:
  1929. a.add(substr(s, c.matches[i][0], c.matches[i][1]))
  1930. assert a == "abcdef"
  1931. #echo expr.rule
  1932. #const filename = "lib/devel/peg/grammar.txt"
  1933. #var grammar = parsePeg(newFileStream(filename, fmRead), filename)
  1934. #echo "a <- [abc]*?".match(grammar)
  1935. assert find("_____abc_______", term("abc"), 2) == 5
  1936. assert match("_______ana", peg"A <- 'ana' / . A")
  1937. assert match("abcs%%%", peg"A <- ..A / .A / '%'")
  1938. var matches: array[0..MaxSubpatterns-1, string]
  1939. if "abc" =~ peg"{'a'}'bc' 'xyz' / {\ident}":
  1940. assert matches[0] == "abc"
  1941. else:
  1942. assert false
  1943. var g2 = peg"""S <- A B / C D
  1944. A <- 'a'+
  1945. B <- 'b'+
  1946. C <- 'c'+
  1947. D <- 'd'+
  1948. """
  1949. assert($g2 == "((A B) / (C D))")
  1950. assert match("cccccdddddd", g2)
  1951. assert("var1=key; var2=key2".replacef(peg"{\ident}'='{\ident}", "$1<-$2$2") ==
  1952. "var1<-keykey; var2<-key2key2")
  1953. assert("var1=key; var2=key2".replace(peg"{\ident}'='{\ident}", "$1<-$2$2") ==
  1954. "$1<-$2$2; $1<-$2$2")
  1955. assert "var1=key; var2=key2".endsWith(peg"{\ident}'='{\ident}")
  1956. if "aaaaaa" =~ peg"'aa' !. / ({'a'})+":
  1957. assert matches[0] == "a"
  1958. else:
  1959. assert false
  1960. if match("abcdefg", peg"c {d} ef {g}", matches, 2):
  1961. assert matches[0] == "d"
  1962. assert matches[1] == "g"
  1963. else:
  1964. assert false
  1965. accum = @[]
  1966. for x in findAll("abcdef", peg".", 3):
  1967. accum.add(x)
  1968. assert(accum == @["d", "e", "f"])
  1969. for x in findAll("abcdef", peg"^{.}", 3):
  1970. assert x == "d"
  1971. if "f(a, b)" =~ peg"{[0-9]+} / ({\ident} '(' {@} ')')":
  1972. assert matches[0] == "f"
  1973. assert matches[1] == "a, b"
  1974. else:
  1975. assert false
  1976. assert match("eine übersicht und außerdem", peg"(\letter \white*)+")
  1977. # ß is not a lower cased letter?!
  1978. assert match("eine übersicht und auerdem", peg"(\lower \white*)+")
  1979. assert match("EINE ÜBERSICHT UND AUSSERDEM", peg"(\upper \white*)+")
  1980. assert(not match("456678", peg"(\letter)+"))
  1981. assert("var1 = key; var2 = key2".replacef(
  1982. peg"\skip(\s*) {\ident}'='{\ident}", "$1<-$2$2") ==
  1983. "var1<-keykey;var2<-key2key2")
  1984. assert match("prefix/start", peg"^start$", 7)
  1985. if "foo" =~ peg"{'a'}?.*":
  1986. assert matches[0].len == 0
  1987. else: assert false
  1988. if "foo" =~ peg"{''}.*":
  1989. assert matches[0] == ""
  1990. else: assert false
  1991. if "foo" =~ peg"{'foo'}":
  1992. assert matches[0] == "foo"
  1993. else: assert false
  1994. let empty_test = peg"^\d*"
  1995. let str = "XYZ"
  1996. assert(str.find(empty_test) == 0)
  1997. assert(str.match(empty_test))
  1998. proc handleMatches*(m: int, n: int, c: openArray[string]): string =
  1999. result = ""
  2000. if m > 0:
  2001. result.add ", "
  2002. result.add case n:
  2003. of 2: toLowerAscii(c[0]) & ": '" & c[1] & "'"
  2004. of 1: toLowerAscii(c[0]) & ": ''"
  2005. else: ""
  2006. assert("Var1=key1;var2=Key2; VAR3".
  2007. replace(peg"{\ident}('='{\ident})* ';'* \s*",
  2008. handleMatches)=="var1: 'key1', var2: 'Key2', var3: ''")
  2009. doAssert "test1".match(peg"""{@}$""")
  2010. doAssert "test2".match(peg"""{(!$ .)*} $""")