pegs.nim 64 KB

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