api-peg.texi 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2006, 2010, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node PEG Parsing
  7. @section PEG Parsing
  8. Parsing Expression Grammars (PEGs) are a way of specifying formal
  9. languages for text processing. They can be used either for matching
  10. (like regular expressions) or for building recursive descent parsers
  11. (like lex/yacc). Guile uses a superset of PEG syntax that allows more
  12. control over what information is preserved during parsing.
  13. Wikipedia has a clear and concise introduction to PEGs if you want to
  14. familiarize yourself with the syntax:
  15. @url{http://en.wikipedia.org/wiki/Parsing_expression_grammar}.
  16. The @code{(ice-9 peg)} module works by compiling PEGs down to lambda
  17. expressions. These can either be stored in variables at compile-time by
  18. the define macros (@code{define-peg-pattern} and
  19. @code{define-peg-string-patterns}) or calculated explicitly at runtime
  20. with the compile functions (@code{compile-peg-pattern} and
  21. @code{peg-string-compile}).
  22. They can then be used for either parsing (@code{match-pattern}) or searching
  23. (@code{search-for-pattern}). For convenience, @code{search-for-pattern}
  24. also takes pattern literals in case you want to inline a simple search
  25. (people often use regular expressions this way).
  26. The rest of this documentation consists of a syntax reference, an API
  27. reference, and a tutorial.
  28. @menu
  29. * PEG Syntax Reference::
  30. * PEG API Reference::
  31. * PEG Tutorial::
  32. * PEG Internals::
  33. @end menu
  34. @node PEG Syntax Reference
  35. @subsection PEG Syntax Reference
  36. @subsubheading Normal PEG Syntax:
  37. @deftp {PEG Pattern} sequence a b
  38. Parses @var{a}. If this succeeds, continues to parse @var{b} from the
  39. end of the text parsed as @var{a}. Succeeds if both @var{a} and
  40. @var{b} succeed.
  41. @code{"a b"}
  42. @code{(and a b)}
  43. @end deftp
  44. @deftp {PEG Pattern} {ordered choice} a b
  45. Parses @var{a}. If this fails, backtracks and parses @var{b}.
  46. Succeeds if either @var{a} or @var{b} succeeds.
  47. @code{"a/b"}
  48. @code{(or a b)}
  49. @end deftp
  50. @deftp {PEG Pattern} {zero or more} a
  51. Parses @var{a} as many times in a row as it can, starting each @var{a}
  52. at the end of the text parsed by the previous @var{a}. Always
  53. succeeds.
  54. @code{"a*"}
  55. @code{(* a)}
  56. @end deftp
  57. @deftp {PEG Pattern} {one or more} a
  58. Parses @var{a} as many times in a row as it can, starting each @var{a}
  59. at the end of the text parsed by the previous @var{a}. Succeeds if at
  60. least one @var{a} was parsed.
  61. @code{"a+"}
  62. @code{(+ a)}
  63. @end deftp
  64. @deftp {PEG Pattern} optional a
  65. Tries to parse @var{a}. Succeeds if @var{a} succeeds.
  66. @code{"a?"}
  67. @code{(? a)}
  68. @end deftp
  69. @deftp {PEG Pattern} {followed by} a
  70. Makes sure it is possible to parse @var{a}, but does not actually parse
  71. it. Succeeds if @var{a} would succeed.
  72. @code{"&a"}
  73. @code{(followed-by a)}
  74. @end deftp
  75. @deftp {PEG Pattern} {not followed by} a
  76. Makes sure it is impossible to parse @var{a}, but does not actually
  77. parse it. Succeeds if @var{a} would fail.
  78. @code{"!a"}
  79. @code{(not-followed-by a)}
  80. @end deftp
  81. @deftp {PEG Pattern} {string literal} ``abc''
  82. Parses the string @var{"abc"}. Succeeds if that parsing succeeds.
  83. @code{"'abc'"}
  84. @code{"abc"}
  85. @end deftp
  86. @deftp {PEG Pattern} {any character}
  87. Parses any single character. Succeeds unless there is no more text to
  88. be parsed.
  89. @code{"."}
  90. @code{peg-any}
  91. @end deftp
  92. @deftp {PEG Pattern} {character class} a b
  93. Alternative syntax for ``Ordered Choice @var{a} @var{b}'' if @var{a} and
  94. @var{b} are characters.
  95. @code{"[ab]"}
  96. @code{(or "a" "b")}
  97. @end deftp
  98. @deftp {PEG Pattern} {range of characters} a z
  99. Parses any character falling between @var{a} and @var{z}.
  100. @code{"[a-z]"}
  101. @code{(range #\a #\z)}
  102. @end deftp
  103. Example:
  104. @example
  105. "(a !b / c &d*) 'e'+"
  106. @end example
  107. Would be:
  108. @lisp
  109. (and
  110. (or
  111. (and a (not-followed-by b))
  112. (and c (followed-by (* d))))
  113. (+ "e"))
  114. @end lisp
  115. @subsubheading Extended Syntax
  116. There is some extra syntax for S-expressions.
  117. @deftp {PEG Pattern} ignore a
  118. Ignore the text matching @var{a}
  119. @end deftp
  120. @deftp {PEG Pattern} capture a
  121. Capture the text matching @var{a}.
  122. @end deftp
  123. @deftp {PEG Pattern} peg a
  124. Embed the PEG pattern @var{a} using string syntax.
  125. @end deftp
  126. Example:
  127. @example
  128. "!a / 'b'"
  129. @end example
  130. Is equivalent to
  131. @lisp
  132. (or (peg "!a") "b")
  133. @end lisp
  134. and
  135. @lisp
  136. (or (not-followed-by a) "b")
  137. @end lisp
  138. @node PEG API Reference
  139. @subsection PEG API Reference
  140. @subsubheading Define Macros
  141. The most straightforward way to define a PEG is by using one of the
  142. define macros (both of these macroexpand into @code{define}
  143. expressions). These macros bind parsing functions to variables. These
  144. parsing functions may be invoked by @code{match-pattern} or
  145. @code{search-for-pattern}, which return a PEG match record. Raw data can be
  146. retrieved from this record with the PEG match deconstructor functions.
  147. More complicated (and perhaps enlightening) examples can be found in the
  148. tutorial.
  149. @deffn {Scheme Macro} define-peg-string-patterns peg-string
  150. Defines all the nonterminals in the PEG @var{peg-string}. More
  151. precisely, @code{define-peg-string-patterns} takes a superset of PEGs. A normal PEG
  152. has a @code{<-} between the nonterminal and the pattern.
  153. @code{define-peg-string-patterns} uses this symbol to determine what information it
  154. should propagate up the parse tree. The normal @code{<-} propagates the
  155. matched text up the parse tree, @code{<--} propagates the matched text
  156. up the parse tree tagged with the name of the nonterminal, and @code{<}
  157. discards that matched text and propagates nothing up the parse tree.
  158. Also, nonterminals may consist of any alphanumeric character or a ``-''
  159. character (in normal PEGs nonterminals can only be alphabetic).
  160. For example, if we:
  161. @lisp
  162. (define-peg-string-patterns
  163. "as <- 'a'+
  164. bs <- 'b'+
  165. as-or-bs <- as/bs")
  166. (define-peg-string-patterns
  167. "as-tag <-- 'a'+
  168. bs-tag <-- 'b'+
  169. as-or-bs-tag <-- as-tag/bs-tag")
  170. @end lisp
  171. Then:
  172. @lisp
  173. (match-pattern as-or-bs "aabbcc") @result{}
  174. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  175. (match-pattern as-or-bs-tag "aabbcc") @result{}
  176. #<peg start: 0 end: 2 string: aabbcc tree: (as-or-bs-tag (as-tag aa))>
  177. @end lisp
  178. Note that in doing this, we have bound 6 variables at the toplevel
  179. (@var{as}, @var{bs}, @var{as-or-bs}, @var{as-tag}, @var{bs-tag}, and
  180. @var{as-or-bs-tag}).
  181. @end deffn
  182. @deffn {Scheme Macro} define-peg-pattern name capture-type peg-sexp
  183. Defines a single nonterminal @var{name}. @var{capture-type} determines
  184. how much information is passed up the parse tree. @var{peg-sexp} is a
  185. PEG in S-expression form.
  186. Possible values for capture-type:
  187. @table @code
  188. @item all
  189. passes the matched text up the parse tree tagged with the name of the
  190. nonterminal.
  191. @item body
  192. passes the matched text up the parse tree.
  193. @item none
  194. passes nothing up the parse tree.
  195. @end table
  196. For Example, if we:
  197. @lisp
  198. (define-peg-pattern as body (+ "a"))
  199. (define-peg-pattern bs body (+ "b"))
  200. (define-peg-pattern as-or-bs body (or as bs))
  201. (define-peg-pattern as-tag all (+ "a"))
  202. (define-peg-pattern bs-tag all (+ "b"))
  203. (define-peg-pattern as-or-bs-tag all (or as-tag bs-tag))
  204. @end lisp
  205. Then:
  206. @lisp
  207. (match-pattern as-or-bs "aabbcc") @result{}
  208. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  209. (match-pattern as-or-bs-tag "aabbcc") @result{}
  210. #<peg start: 0 end: 2 string: aabbcc tree: (as-or-bs-tag (as-tag aa))>
  211. @end lisp
  212. Note that in doing this, we have bound 6 variables at the toplevel
  213. (@var{as}, @var{bs}, @var{as-or-bs}, @var{as-tag}, @var{bs-tag}, and
  214. @var{as-or-bs-tag}).
  215. @end deffn
  216. @subsubheading Compile Functions
  217. It is sometimes useful to be able to compile anonymous PEG patterns at
  218. runtime. These functions let you do that using either syntax.
  219. @deffn {Scheme Procedure} peg-string-compile peg-string capture-type
  220. Compiles the PEG pattern in @var{peg-string} propagating according to
  221. @var{capture-type} (capture-type can be any of the values from
  222. @code{define-peg-pattern}).
  223. @end deffn
  224. @deffn {Scheme Procedure} compile-peg-pattern peg-sexp capture-type
  225. Compiles the PEG pattern in @var{peg-sexp} propagating according to
  226. @var{capture-type} (capture-type can be any of the values from
  227. @code{define-peg-pattern}).
  228. @end deffn
  229. The functions return syntax objects, which can be useful if you want to
  230. use them in macros. If all you want is to define a new nonterminal, you
  231. can do the following:
  232. @lisp
  233. (define exp '(+ "a"))
  234. (define as (compile (compile-peg-pattern exp 'body)))
  235. @end lisp
  236. You can use this nonterminal with all of the regular PEG functions:
  237. @lisp
  238. (match-pattern as "aaaaa") @result{}
  239. #<peg start: 0 end: 5 string: bbbbb tree: bbbbb>
  240. @end lisp
  241. @subsubheading Parsing & Matching Functions
  242. For our purposes, ``parsing'' means parsing a string into a tree
  243. starting from the first character, while ``matching'' means searching
  244. through the string for a substring. In practice, the only difference
  245. between the two functions is that @code{match-pattern} gives up if it can't
  246. find a valid substring starting at index 0 and @code{search-for-pattern} keeps
  247. looking. They are both equally capable of ``parsing'' and ``matching''
  248. given those constraints.
  249. @deffn {Scheme Procedure} match-pattern nonterm string
  250. Parses @var{string} using the PEG stored in @var{nonterm}. If no match
  251. was found, @code{match-pattern} returns false. If a match was found, a PEG
  252. match record is returned.
  253. The @code{capture-type} argument to @code{define-peg-pattern} allows you to
  254. choose what information to hold on to while parsing. The options are:
  255. @table @code
  256. @item all
  257. tag the matched text with the nonterminal
  258. @item body
  259. just the matched text
  260. @item none
  261. nothing
  262. @end table
  263. @lisp
  264. (define-peg-pattern as all (+ "a"))
  265. (match-pattern as "aabbcc") @result{}
  266. #<peg start: 0 end: 2 string: aabbcc tree: (as aa)>
  267. (define-peg-pattern as body (+ "a"))
  268. (match-pattern as "aabbcc") @result{}
  269. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  270. (define-peg-pattern as none (+ "a"))
  271. (match-pattern as "aabbcc") @result{}
  272. #<peg start: 0 end: 2 string: aabbcc tree: ()>
  273. (define-peg-pattern bs body (+ "b"))
  274. (match-pattern bs "aabbcc") @result{}
  275. #f
  276. @end lisp
  277. @end deffn
  278. @deffn {Scheme Macro} search-for-pattern nonterm-or-peg string
  279. Searches through @var{string} looking for a matching subexpression.
  280. @var{nonterm-or-peg} can either be a nonterminal or a literal PEG
  281. pattern. When a literal PEG pattern is provided, @code{search-for-pattern} works
  282. very similarly to the regular expression searches many hackers are used
  283. to. If no match was found, @code{search-for-pattern} returns false. If a match
  284. was found, a PEG match record is returned.
  285. @lisp
  286. (define-peg-pattern as body (+ "a"))
  287. (search-for-pattern as "aabbcc") @result{}
  288. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  289. (search-for-pattern (+ "a") "aabbcc") @result{}
  290. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  291. (search-for-pattern "'a'+" "aabbcc") @result{}
  292. #<peg start: 0 end: 2 string: aabbcc tree: aa>
  293. (define-peg-pattern as all (+ "a"))
  294. (search-for-pattern as "aabbcc") @result{}
  295. #<peg start: 0 end: 2 string: aabbcc tree: (as aa)>
  296. (define-peg-pattern bs body (+ "b"))
  297. (search-for-pattern bs "aabbcc") @result{}
  298. #<peg start: 2 end: 4 string: aabbcc tree: bb>
  299. (search-for-pattern (+ "b") "aabbcc") @result{}
  300. #<peg start: 2 end: 4 string: aabbcc tree: bb>
  301. (search-for-pattern "'b'+" "aabbcc") @result{}
  302. #<peg start: 2 end: 4 string: aabbcc tree: bb>
  303. (define-peg-pattern zs body (+ "z"))
  304. (search-for-pattern zs "aabbcc") @result{}
  305. #f
  306. (search-for-pattern (+ "z") "aabbcc") @result{}
  307. #f
  308. (search-for-pattern "'z'+" "aabbcc") @result{}
  309. #f
  310. @end lisp
  311. @end deffn
  312. @subsubheading PEG Match Records
  313. The @code{match-pattern} and @code{search-for-pattern} functions both return PEG
  314. match records. Actual information can be extracted from these with the
  315. following functions.
  316. @deffn {Scheme Procedure} peg:string match-record
  317. Returns the original string that was parsed in the creation of
  318. @code{match-record}.
  319. @end deffn
  320. @deffn {Scheme Procedure} peg:start match-record
  321. Returns the index of the first parsed character in the original string
  322. (from @code{peg:string}). If this is the same as @code{peg:end},
  323. nothing was parsed.
  324. @end deffn
  325. @deffn {Scheme Procedure} peg:end match-record
  326. Returns one more than the index of the last parsed character in the
  327. original string (from @code{peg:string}). If this is the same as
  328. @code{peg:start}, nothing was parsed.
  329. @end deffn
  330. @deffn {Scheme Procedure} peg:substring match-record
  331. Returns the substring parsed by @code{match-record}. This is equivalent to
  332. @code{(substring (peg:string match-record) (peg:start match-record) (peg:end
  333. match-record))}.
  334. @end deffn
  335. @deffn {Scheme Procedure} peg:tree match-record
  336. Returns the tree parsed by @code{match-record}.
  337. @end deffn
  338. @deffn {Scheme Procedure} peg-record? match-record
  339. Returns true if @code{match-record} is a PEG match record, or false
  340. otherwise.
  341. @end deffn
  342. Example:
  343. @lisp
  344. (define-peg-pattern bs all (peg "'b'+"))
  345. (search-for-pattern bs "aabbcc") @result{}
  346. #<peg start: 2 end: 4 string: aabbcc tree: (bs bb)>
  347. (let ((pm (search-for-pattern bs "aabbcc")))
  348. `((string ,(peg:string pm))
  349. (start ,(peg:start pm))
  350. (end ,(peg:end pm))
  351. (substring ,(peg:substring pm))
  352. (tree ,(peg:tree pm))
  353. (record? ,(peg-record? pm)))) @result{}
  354. ((string "aabbcc")
  355. (start 2)
  356. (end 4)
  357. (substring "bb")
  358. (tree (bs "bb"))
  359. (record? #t))
  360. @end lisp
  361. @subsubheading Miscellaneous
  362. @deffn {Scheme Procedure} context-flatten tst lst
  363. Takes a predicate @var{tst} and a list @var{lst}. Flattens @var{lst}
  364. until all elements are either atoms or satisfy @var{tst}. If @var{lst}
  365. itself satisfies @var{tst}, @code{(list lst)} is returned (this is a
  366. flat list whose only element satisfies @var{tst}).
  367. @lisp
  368. (context-flatten (lambda (x) (and (number? (car x)) (= (car x) 1))) '(2 2 (1 1 (2 2)) (2 2 (1 1)))) @result{}
  369. (2 2 (1 1 (2 2)) 2 2 (1 1))
  370. (context-flatten (lambda (x) (and (number? (car x)) (= (car x) 1))) '(1 1 (1 1 (2 2)) (2 2 (1 1)))) @result{}
  371. ((1 1 (1 1 (2 2)) (2 2 (1 1))))
  372. @end lisp
  373. If you're wondering why this is here, take a look at the tutorial.
  374. @end deffn
  375. @deffn {Scheme Procedure} keyword-flatten terms lst
  376. A less general form of @code{context-flatten}. Takes a list of terminal
  377. atoms @code{terms} and flattens @var{lst} until all elements are either
  378. atoms, or lists which have an atom from @code{terms} as their first
  379. element.
  380. @lisp
  381. (keyword-flatten '(a b) '(c a b (a c) (b c) (c (b a) (c a)))) @result{}
  382. (c a b (a c) (b c) c (b a) c a)
  383. @end lisp
  384. If you're wondering why this is here, take a look at the tutorial.
  385. @end deffn
  386. @node PEG Tutorial
  387. @subsection PEG Tutorial
  388. @subsubheading Parsing /etc/passwd
  389. This example will show how to parse /etc/passwd using PEGs.
  390. First we define an example /etc/passwd file:
  391. @lisp
  392. (define *etc-passwd*
  393. "root:x:0:0:root:/root:/bin/bash
  394. daemon:x:1:1:daemon:/usr/sbin:/bin/sh
  395. bin:x:2:2:bin:/bin:/bin/sh
  396. sys:x:3:3:sys:/dev:/bin/sh
  397. nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
  398. messagebus:x:103:107::/var/run/dbus:/bin/false
  399. ")
  400. @end lisp
  401. As a first pass at this, we might want to have all the entries in
  402. /etc/passwd in a list.
  403. Doing this with string-based PEG syntax would look like this:
  404. @lisp
  405. (define-peg-string-patterns
  406. "passwd <- entry* !.
  407. entry <-- (! NL .)* NL*
  408. NL < '\n'")
  409. @end lisp
  410. A @code{passwd} file is 0 or more entries (@code{entry*}) until the end
  411. of the file (@code{!.} (@code{.} is any character, so @code{!.} means
  412. ``not anything'')). We want to capture the data in the nonterminal
  413. @code{passwd}, but not tag it with the name, so we use @code{<-}.
  414. An entry is a series of 0 or more characters that aren't newlines
  415. (@code{(! NL .)*}) followed by 0 or more newlines (@code{NL*}). We want
  416. to tag all the entries with @code{entry}, so we use @code{<--}.
  417. A newline is just a literal newline (@code{'\n'}). We don't want a
  418. bunch of newlines cluttering up the output, so we use @code{<} to throw
  419. away the captured data.
  420. Here is the same PEG defined using S-expressions:
  421. @lisp
  422. (define-peg-pattern passwd body (and (* entry) (not-followed-by peg-any)))
  423. (define-peg-pattern entry all (and (* (and (not-followed-by NL) peg-any))
  424. (* NL)))
  425. (define-peg-pattern NL none "\n")
  426. @end lisp
  427. Obviously this is much more verbose. On the other hand, it's more
  428. explicit, and thus easier to build automatically. However, there are
  429. some tricks that make S-expressions easier to use in some cases. One is
  430. the @code{ignore} keyword; the string syntax has no way to say ``throw
  431. away this text'' except breaking it out into a separate nonterminal.
  432. For instance, to throw away the newlines we had to define @code{NL}. In
  433. the S-expression syntax, we could have simply written @code{(ignore
  434. "\n")}. Also, for the cases where string syntax is really much cleaner,
  435. the @code{peg} keyword can be used to embed string syntax in
  436. S-expression syntax. For instance, we could have written:
  437. @lisp
  438. (define-peg-pattern passwd body (peg "entry* !."))
  439. @end lisp
  440. However we define it, parsing @code{*etc-passwd*} with the @code{passwd}
  441. nonterminal yields the same results:
  442. @lisp
  443. (peg:tree (match-pattern passwd *etc-passwd*)) @result{}
  444. ((entry "root:x:0:0:root:/root:/bin/bash")
  445. (entry "daemon:x:1:1:daemon:/usr/sbin:/bin/sh")
  446. (entry "bin:x:2:2:bin:/bin:/bin/sh")
  447. (entry "sys:x:3:3:sys:/dev:/bin/sh")
  448. (entry "nobody:x:65534:65534:nobody:/nonexistent:/bin/sh")
  449. (entry "messagebus:x:103:107::/var/run/dbus:/bin/false"))
  450. @end lisp
  451. However, here is something to be wary of:
  452. @lisp
  453. (peg:tree (match-pattern passwd "one entry")) @result{}
  454. (entry "one entry")
  455. @end lisp
  456. By default, the parse trees generated by PEGs are compressed as much as
  457. possible without losing information. It may not look like this is what
  458. you want at first, but uncompressed parse trees are an enormous headache
  459. (there's no easy way to predict how deep particular lists will nest,
  460. there are empty lists littered everywhere, etc. etc.). One side-effect
  461. of this, however, is that sometimes the compressor is too aggressive.
  462. No information is discarded when @code{((entry "one entry"))} is
  463. compressed to @code{(entry "one entry")}, but in this particular case it
  464. probably isn't what we want.
  465. There are two functions for easily dealing with this:
  466. @code{keyword-flatten} and @code{context-flatten}. The
  467. @code{keyword-flatten} function takes a list of keywords and a list to
  468. flatten, then tries to coerce the list such that the first element of
  469. all sublists is one of the keywords. The @code{context-flatten}
  470. function is similar, but instead of a list of keywords it takes a
  471. predicate that should indicate whether a given sublist is good enough
  472. (refer to the API reference for more details).
  473. What we want here is @code{keyword-flatten}.
  474. @lisp
  475. (keyword-flatten '(entry) (peg:tree (match-pattern passwd *etc-passwd*))) @result{}
  476. ((entry "root:x:0:0:root:/root:/bin/bash")
  477. (entry "daemon:x:1:1:daemon:/usr/sbin:/bin/sh")
  478. (entry "bin:x:2:2:bin:/bin:/bin/sh")
  479. (entry "sys:x:3:3:sys:/dev:/bin/sh")
  480. (entry "nobody:x:65534:65534:nobody:/nonexistent:/bin/sh")
  481. (entry "messagebus:x:103:107::/var/run/dbus:/bin/false"))
  482. (keyword-flatten '(entry) (peg:tree (match-pattern passwd "one entry"))) @result{}
  483. ((entry "one entry"))
  484. @end lisp
  485. Of course, this is a somewhat contrived example. In practice we would
  486. probably just tag the @code{passwd} nonterminal to remove the ambiguity
  487. (using either the @code{all} keyword for S-expressions or the @code{<--}
  488. symbol for strings)..
  489. @lisp
  490. (define-peg-pattern tag-passwd all (peg "entry* !."))
  491. (peg:tree (match-pattern tag-passwd *etc-passwd*)) @result{}
  492. (tag-passwd
  493. (entry "root:x:0:0:root:/root:/bin/bash")
  494. (entry "daemon:x:1:1:daemon:/usr/sbin:/bin/sh")
  495. (entry "bin:x:2:2:bin:/bin:/bin/sh")
  496. (entry "sys:x:3:3:sys:/dev:/bin/sh")
  497. (entry "nobody:x:65534:65534:nobody:/nonexistent:/bin/sh")
  498. (entry "messagebus:x:103:107::/var/run/dbus:/bin/false"))
  499. (peg:tree (match-pattern tag-passwd "one entry"))
  500. (tag-passwd
  501. (entry "one entry"))
  502. @end lisp
  503. If you're ever uncertain about the potential results of parsing
  504. something, remember the two absolute rules:
  505. @enumerate
  506. @item
  507. No parsing information will ever be discarded.
  508. @item
  509. There will never be any lists with fewer than 2 elements.
  510. @end enumerate
  511. For the purposes of (1), "parsing information" means things tagged with
  512. the @code{any} keyword or the @code{<--} symbol. Plain strings will be
  513. concatenated.
  514. Let's extend this example a bit more and actually pull some useful
  515. information out of the passwd file:
  516. @lisp
  517. (define-peg-string-patterns
  518. "passwd <-- entry* !.
  519. entry <-- login C pass C uid C gid C nameORcomment C homedir C shell NL*
  520. login <-- text
  521. pass <-- text
  522. uid <-- [0-9]*
  523. gid <-- [0-9]*
  524. nameORcomment <-- text
  525. homedir <-- path
  526. shell <-- path
  527. path <-- (SLASH pathELEMENT)*
  528. pathELEMENT <-- (!NL !C !'/' .)*
  529. text <- (!NL !C .)*
  530. C < ':'
  531. NL < '\n'
  532. SLASH < '/'")
  533. @end lisp
  534. This produces rather pretty parse trees:
  535. @lisp
  536. (passwd
  537. (entry (login "root")
  538. (pass "x")
  539. (uid "0")
  540. (gid "0")
  541. (nameORcomment "root")
  542. (homedir (path (pathELEMENT "root")))
  543. (shell (path (pathELEMENT "bin") (pathELEMENT "bash"))))
  544. (entry (login "daemon")
  545. (pass "x")
  546. (uid "1")
  547. (gid "1")
  548. (nameORcomment "daemon")
  549. (homedir
  550. (path (pathELEMENT "usr") (pathELEMENT "sbin")))
  551. (shell (path (pathELEMENT "bin") (pathELEMENT "sh"))))
  552. (entry (login "bin")
  553. (pass "x")
  554. (uid "2")
  555. (gid "2")
  556. (nameORcomment "bin")
  557. (homedir (path (pathELEMENT "bin")))
  558. (shell (path (pathELEMENT "bin") (pathELEMENT "sh"))))
  559. (entry (login "sys")
  560. (pass "x")
  561. (uid "3")
  562. (gid "3")
  563. (nameORcomment "sys")
  564. (homedir (path (pathELEMENT "dev")))
  565. (shell (path (pathELEMENT "bin") (pathELEMENT "sh"))))
  566. (entry (login "nobody")
  567. (pass "x")
  568. (uid "65534")
  569. (gid "65534")
  570. (nameORcomment "nobody")
  571. (homedir (path (pathELEMENT "nonexistent")))
  572. (shell (path (pathELEMENT "bin") (pathELEMENT "sh"))))
  573. (entry (login "messagebus")
  574. (pass "x")
  575. (uid "103")
  576. (gid "107")
  577. nameORcomment
  578. (homedir
  579. (path (pathELEMENT "var")
  580. (pathELEMENT "run")
  581. (pathELEMENT "dbus")))
  582. (shell (path (pathELEMENT "bin") (pathELEMENT "false")))))
  583. @end lisp
  584. Notice that when there's no entry in a field (e.g. @code{nameORcomment}
  585. for messagebus) the symbol is inserted. This is the ``don't throw away
  586. any information'' rule---we succesfully matched a @code{nameORcomment}
  587. of 0 characters (since we used @code{*} when defining it). This is
  588. usually what you want, because it allows you to e.g. use @code{list-ref}
  589. to pull out elements (since they all have known offsets).
  590. If you'd prefer not to have symbols for empty matches, you can replace
  591. the @code{*} with a @code{+} and add a @code{?} after the
  592. @code{nameORcomment} in @code{entry}. Then it will try to parse 1 or
  593. more characters, fail (inserting nothing into the parse tree), but
  594. continue because it didn't have to match the nameORcomment to continue.
  595. @subsubheading Embedding Arithmetic Expressions
  596. We can parse simple mathematical expressions with the following PEG:
  597. @lisp
  598. (define-peg-string-patterns
  599. "expr <- sum
  600. sum <-- (product ('+' / '-') sum) / product
  601. product <-- (value ('*' / '/') product) / value
  602. value <-- number / '(' expr ')'
  603. number <-- [0-9]+")
  604. @end lisp
  605. Then:
  606. @lisp
  607. (peg:tree (match-pattern expr "1+1/2*3+(1+1)/2")) @result{}
  608. (sum (product (value (number "1")))
  609. "+"
  610. (sum (product
  611. (value (number "1"))
  612. "/"
  613. (product
  614. (value (number "2"))
  615. "*"
  616. (product (value (number "3")))))
  617. "+"
  618. (sum (product
  619. (value "("
  620. (sum (product (value (number "1")))
  621. "+"
  622. (sum (product (value (number "1")))))
  623. ")")
  624. "/"
  625. (product (value (number "2")))))))
  626. @end lisp
  627. There is very little wasted effort in this PEG. The @code{number}
  628. nonterminal has to be tagged because otherwise the numbers might run
  629. together with the arithmetic expressions during the string concatenation
  630. stage of parse-tree compression (the parser will see ``1'' followed by
  631. ``/'' and decide to call it ``1/''). When in doubt, tag.
  632. It is very easy to turn these parse trees into lisp expressions:
  633. @lisp
  634. (define (parse-sum sum left . rest)
  635. (if (null? rest)
  636. (apply parse-product left)
  637. (list (string->symbol (car rest))
  638. (apply parse-product left)
  639. (apply parse-sum (cadr rest)))))
  640. (define (parse-product product left . rest)
  641. (if (null? rest)
  642. (apply parse-value left)
  643. (list (string->symbol (car rest))
  644. (apply parse-value left)
  645. (apply parse-product (cadr rest)))))
  646. (define (parse-value value first . rest)
  647. (if (null? rest)
  648. (string->number (cadr first))
  649. (apply parse-sum (car rest))))
  650. (define parse-expr parse-sum)
  651. @end lisp
  652. (Notice all these functions look very similar; for a more complicated
  653. PEG, it would be worth abstracting.)
  654. Then:
  655. @lisp
  656. (apply parse-expr (peg:tree (match-pattern expr "1+1/2*3+(1+1)/2"))) @result{}
  657. (+ 1 (+ (/ 1 (* 2 3)) (/ (+ 1 1) 2)))
  658. @end lisp
  659. But wait! The associativity is wrong! Where it says @code{(/ 1 (* 2
  660. 3))}, it should say @code{(* (/ 1 2) 3)}.
  661. It's tempting to try replacing e.g. @code{"sum <-- (product ('+' / '-')
  662. sum) / product"} with @code{"sum <-- (sum ('+' / '-') product) /
  663. product"}, but this is a Bad Idea. PEGs don't support left recursion.
  664. To see why, imagine what the parser will do here. When it tries to
  665. parse @code{sum}, it first has to try and parse @code{sum}. But to do
  666. that, it first has to try and parse @code{sum}. This will continue
  667. until the stack gets blown off.
  668. So how does one parse left-associative binary operators with PEGs?
  669. Honestly, this is one of their major shortcomings. There's no
  670. general-purpose way of doing this, but here the repetition operators are
  671. a good choice:
  672. @lisp
  673. (use-modules (srfi srfi-1))
  674. (define-peg-string-patterns
  675. "expr <- sum
  676. sum <-- (product ('+' / '-'))* product
  677. product <-- (value ('*' / '/'))* value
  678. value <-- number / '(' expr ')'
  679. number <-- [0-9]+")
  680. ;; take a deep breath...
  681. (define (make-left-parser next-func)
  682. (lambda (sum first . rest) ;; general form, comments below assume
  683. ;; that we're dealing with a sum expression
  684. (if (null? rest) ;; form (sum (product ...))
  685. (apply next-func first)
  686. (if (string? (cadr first));; form (sum ((product ...) "+") (product ...))
  687. (list (string->symbol (cadr first))
  688. (apply next-func (car first))
  689. (apply next-func (car rest)))
  690. ;; form (sum (((product ...) "+") ((product ...) "+")) (product ...))
  691. (car
  692. (reduce ;; walk through the list and build a left-associative tree
  693. (lambda (l r)
  694. (list (list (cadr r) (car r) (apply next-func (car l)))
  695. (string->symbol (cadr l))))
  696. 'ignore
  697. (append ;; make a list of all the products
  698. ;; the first one should be pre-parsed
  699. (list (list (apply next-func (caar first))
  700. (string->symbol (cadar first))))
  701. (cdr first)
  702. ;; the last one has to be added in
  703. (list (append rest '("done"))))))))))
  704. (define (parse-value value first . rest)
  705. (if (null? rest)
  706. (string->number (cadr first))
  707. (apply parse-sum (car rest))))
  708. (define parse-product (make-left-parser parse-value))
  709. (define parse-sum (make-left-parser parse-product))
  710. (define parse-expr parse-sum)
  711. @end lisp
  712. Then:
  713. @lisp
  714. (apply parse-expr (peg:tree (match-pattern expr "1+1/2*3+(1+1)/2"))) @result{}
  715. (+ (+ 1 (* (/ 1 2) 3)) (/ (+ 1 1) 2))
  716. @end lisp
  717. As you can see, this is much uglier (it could be made prettier by using
  718. @code{context-flatten}, but the way it's written above makes it clear
  719. how we deal with the three ways the zero-or-more @code{*} expression can
  720. parse). Fortunately, most of the time we can get away with only using
  721. right-associativity.
  722. @subsubheading Simplified Functions
  723. For a more tantalizing example, consider the following grammar that
  724. parses (highly) simplified C functions:
  725. @lisp
  726. (define-peg-string-patterns
  727. "cfunc <-- cSP ctype cSP cname cSP cargs cLB cSP cbody cRB
  728. ctype <-- cidentifier
  729. cname <-- cidentifier
  730. cargs <-- cLP (! (cSP cRP) carg cSP (cCOMMA / cRP) cSP)* cSP
  731. carg <-- cSP ctype cSP cname
  732. cbody <-- cstatement *
  733. cidentifier <- [a-zA-z][a-zA-Z0-9_]*
  734. cstatement <-- (!';'.)*cSC cSP
  735. cSC < ';'
  736. cCOMMA < ','
  737. cLP < '('
  738. cRP < ')'
  739. cLB < '@{'
  740. cRB < '@}'
  741. cSP < [ \t\n]*")
  742. @end lisp
  743. Then:
  744. @lisp
  745. (match-pattern cfunc "int square(int a) @{ return a*a;@}") @result{}
  746. (32
  747. (cfunc (ctype "int")
  748. (cname "square")
  749. (cargs (carg (ctype "int") (cname "a")))
  750. (cbody (cstatement "return a*a"))))
  751. @end lisp
  752. And:
  753. @lisp
  754. (match-pattern cfunc "int mod(int a, int b) @{ int c = a/b;return a-b*c; @}") @result{}
  755. (52
  756. (cfunc (ctype "int")
  757. (cname "mod")
  758. (cargs (carg (ctype "int") (cname "a"))
  759. (carg (ctype "int") (cname "b")))
  760. (cbody (cstatement "int c = a/b")
  761. (cstatement "return a- b*c"))))
  762. @end lisp
  763. By wrapping all the @code{carg} nonterminals in a @code{cargs}
  764. nonterminal, we were able to remove any ambiguity in the parsing
  765. structure and avoid having to call @code{context-flatten} on the output
  766. of @code{match-pattern}. We used the same trick with the @code{cstatement}
  767. nonterminals, wrapping them in a @code{cbody} nonterminal.
  768. The whitespace nonterminal @code{cSP} used here is a (very) useful
  769. instantiation of a common pattern for matching syntactically irrelevant
  770. information. Since it's tagged with @code{<} and ends with @code{*} it
  771. won't clutter up the parse trees (all the empty lists will be discarded
  772. during the compression step) and it will never cause parsing to fail.
  773. @node PEG Internals
  774. @subsection PEG Internals
  775. A PEG parser takes a string as input and attempts to parse it as a given
  776. nonterminal. The key idea of the PEG implementation is that every
  777. nonterminal is just a function that takes a string as an argument and
  778. attempts to parse that string as its nonterminal. The functions always
  779. start from the beginning, but a parse is considered successful if there
  780. is material left over at the end.
  781. This makes it easy to model different PEG parsing operations. For
  782. instance, consider the PEG grammar @code{"ab"}, which could also be
  783. written @code{(and "a" "b")}. It matches the string ``ab''. Here's how
  784. that might be implemented in the PEG style:
  785. @lisp
  786. (define (match-and-a-b str)
  787. (match-a str)
  788. (match-b str))
  789. @end lisp
  790. As you can see, the use of functions provides an easy way to model
  791. sequencing. In a similar way, one could model @code{(or a b)} with
  792. something like the following:
  793. @lisp
  794. (define (match-or-a-b str)
  795. (or (match-a str) (match-b str)))
  796. @end lisp
  797. Here the semantics of a PEG @code{or} expression map naturally onto
  798. Scheme's @code{or} operator. This function will attempt to run
  799. @code{(match-a str)}, and return its result if it succeeds. Otherwise it
  800. will run @code{(match-b str)}.
  801. Of course, the code above wouldn't quite work. We need some way for the
  802. parsing functions to communicate. The actual interface used is below.
  803. @subsubheading Parsing Function Interface
  804. A parsing function takes three arguments - a string, the length of that
  805. string, and the position in that string it should start parsing at. In
  806. effect, the parsing functions pass around substrings in pieces - the
  807. first argument is a buffer of characters, and the second two give a
  808. range within that buffer that the parsing function should look at.
  809. Parsing functions return either #f, if they failed to match their
  810. nonterminal, or a list whose first element must be an integer
  811. representing the final position in the string they matched and whose cdr
  812. can be any other data the function wishes to return, or '() if it
  813. doesn't have any more data.
  814. The one caveat is that if the extra data it returns is a list, any
  815. adjacent strings in that list will be appended by @code{match-pattern}. For
  816. instance, if a parsing function returns @code{(13 ("a" "b" "c"))},
  817. @code{match-pattern} will take @code{(13 ("abc"))} as its value.
  818. For example, here is a function to match ``ab'' using the actual
  819. interface.
  820. @lisp
  821. (define (match-a-b str len pos)
  822. (and (<= (+ pos 2) len)
  823. (string= str "ab" pos (+ pos 2))
  824. (list (+ pos 2) '()))) ; we return no extra information
  825. @end lisp
  826. The above function can be used to match a string by running
  827. @code{(match-pattern match-a-b "ab")}.
  828. @subsubheading Code Generators and Extensible Syntax
  829. PEG expressions, such as those in a @code{define-peg-pattern} form, are
  830. interpreted internally in two steps.
  831. First, any string PEG is expanded into an s-expression PEG by the code
  832. in the @code{(ice-9 peg string-peg)} module.
  833. Then, the s-expression PEG that results is compiled into a parsing
  834. function by the @code{(ice-9 peg codegen)} module. In particular, the
  835. function @code{compile-peg-pattern} is called on the s-expression. It then
  836. decides what to do based on the form it is passed.
  837. The PEG syntax can be expanded by providing @code{compile-peg-pattern} more
  838. options for what to do with its forms. The extended syntax will be
  839. associated with a symbol, for instance @code{my-parsing-form}, and will
  840. be called on all PEG expressions of the form
  841. @lisp
  842. (my-parsing-form ...)
  843. @end lisp
  844. The parsing function should take two arguments. The first will be a
  845. syntax object containing a list with all of the arguments to the form
  846. (but not the form's name), and the second will be the
  847. @code{capture-type} argument that is passed to @code{define-peg-pattern}.
  848. New functions can be registered by calling @code{(add-peg-compiler!
  849. symbol function)}, where @code{symbol} is the symbol that will indicate
  850. a form of this type and @code{function} is the code generating function
  851. described above. The function @code{add-peg-compiler!} is exported from
  852. the @code{(ice-9 peg codegen)} module.