11
22# PikaParser.jl
33
4+ | Build status | Documentation |
5+ | :---:| :---:|
6+ | ![ CI status] ( https://github.com/LCSB-BioCore/PikaParser.jl/workflows/CI/badge.svg?branch=master ) [ ![ codecov] ( https://codecov.io/gh/LCSB-BioCore/PikaParser.jl/branch/master/graph/badge.svg?token=A2ui7exGIH )] ( https://codecov.io/gh/LCSB-BioCore/PikaParser.jl ) | [ ![ stable documentation] ( https://img.shields.io/badge/docs-stable-blue )] ( https://lcsb-biocore.github.io/PikaParser.jl/stable ) [ ![ dev documentation] ( https://img.shields.io/badge/docs-dev-cyan )] ( https://lcsb-biocore.github.io/PikaParser.jl/dev ) |
7+
48A simple straightforward implementation of PikaParser in pure Julia, following
59the specification by Luke A. D. Hutchison (see
610https://github.com/lukehutch/pikaparser ).
@@ -43,21 +47,24 @@ rules = Dict(
4347
4448g = P. make_grammar (
4549 [:expr ], # the top-level rule
46- P. flatten (rules),
50+ P. flatten (rules, Char), # process the rules into a single level and specialize them for crunching Chars
4751)
4852```
4953
5054The grammar is now prepared for parsing.
5155
5256### Parsing text
5357
54- Pika parsers require frequent indexing of the input, Strings thus need to be
55- converted to character vectors to be usable as parser input. (To improve
56- performance, it is advisable to lex your input into a vector of more complex
57- tokens.)
58+ Parsing is executed simply by running your grammar on any indexable input using
59+ ` parse ` .
60+
61+ (Notably, PikaParsers require frequent indexing of inputs, and incremental
62+ parsing of streams is thus complicated. To improve the performance, it is also
63+ advisable to lex your input into a vector of more complex tokens, using e.g.
64+ ` parse_lex ` .)
5865
5966``` julia
60- input = collect ( " 12-(34+567-8)" )
67+ input = " 12-(34+567-8)"
6168p = P. parse (g, input)
6269```
6370
@@ -67,7 +74,7 @@ P.find_match_at!(p, :expr, 1)
6774```
6875...which returns an index in the match table (if found), such as ` 45 ` .
6976
70- You can have a look at the match. ` p.matches[45] ` should return:
77+ You can have a look at the match: ` p.matches[45] ` should return:
7178``` julia
7279PikaParser. Match (10 , 1 , 13 , 2 , [44 ])
7380```
@@ -89,25 +96,25 @@ JuliaFormatter, you will get something like:
8996``` julia
9097expr (
9198 minusexpr (
92- expr (digits (digit (' 1 ' ), digit (' 2 ' ))),
93- var"minusexpr-2" (' - ' ),
99+ expr (digits (digit (" 1 " ), digit (" 2 " ))),
100+ var"minusexpr-2" (" - " ),
94101 expr (
95102 parens (
96- var"parens-1" (' ( ' ),
103+ var"parens-1" (" ( " ),
97104 expr (
98105 plusexpr (
99- expr (digits (digit (' 3 ' ), digit (' 4 ' ))),
100- var"plusexpr-2" (' + ' ),
106+ expr (digits (digit (" 3 " ), digit (" 4 " ))),
107+ var"plusexpr-2" (" + " ),
101108 expr (
102109 minusexpr (
103- expr (digits (digit (' 5 ' ), digit (' 6 ' ), digit (' 7 ' ))),
104- var"minusexpr-2" (' - ' ),
105- expr (digits (digit (' 8 ' ))),
110+ expr (digits (digit (" 5 " ), digit (" 6 " ), digit (" 7 " ))),
111+ var"minusexpr-2" (" - " ),
112+ expr (digits (digit (" 8 " ))),
106113 ),
107114 ),
108115 ),
109116 ),
110- var"parens-3" (' ) ' ),
117+ var"parens-3" (" ) " ),
111118 ),
112119 ),
113120 ),
@@ -120,7 +127,7 @@ evaluate the expression as follows:
120127``` julia
121128P. traverse_match (p, P. find_match_at! (p, :expr , 1 ),
122129 fold = (m, p, subvals) ->
123- m. rule == :digits ? parse (Int, String ( m. view) ) :
130+ m. rule == :digits ? parse (Int, m. view) :
124131 m. rule == :expr ? subvals[1 ] :
125132 m. rule == :parens ? subvals[2 ] :
126133 m. rule == :plusexpr ? subvals[1 ] + subvals[3 ] :
0 commit comments