Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
546 changes: 376 additions & 170 deletions examples/calculator/grammar_encoded_prec_parser.py

Large diffs are not rendered by default.

445 changes: 299 additions & 146 deletions examples/calculator/parser.py

Large diffs are not rendered by default.

1,715 changes: 1,092 additions & 623 deletions examples/jsonpath/parser.py

Large diffs are not rendered by default.

138 changes: 68 additions & 70 deletions tests/grammars/grammar.pest
Original file line number Diff line number Diff line change
@@ -1,94 +1,92 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

string = { "abc" }
insensitive = { ^"abc" }
range = { '0'..'9' }
ident = { string }
pos_pred = { &string }
neg_pred = { !string }
double_neg_pred = { !!string }
sequence = !{ string ~ string }
sequence_compound = ${ string ~ string }
sequence_atomic = @{ string ~ string }
sequence_non_atomic = @{ sequence }
sequence_atomic_compound = @{ sequence_compound }
sequence_nested = { string ~ string }
sequence_compound_nested = ${ sequence_nested }
node_tag = { #string = string }
choice = { string | range }
choice_prefix = { | string | range }
optional = { string? }
repeat = { string* }
repeat_atomic = @{ string* }
repeat_once = { string+ }
repeat_once_atomic = @{ string+ }
repeat_min_max = { string{2, 3} }
repeat_min_max_atomic = @{ string{2, 3} }
repeat_exact = { string{2} }
repeat_min = { string{2,} }
repeat_min_atomic = @{ string{2,} }
repeat_max = { string{, 2} }
repeat_max_atomic = @{ string{, 2} }
soi_at_start = { SOI ~ string }
repeat_mutate_stack = { (PUSH('a'..'c') ~ ",")* ~ POP ~ POP ~ POP }
repeat_mutate_stack_pop_all = { (PUSH('a'..'c') ~ ",")* ~ POP_ALL }
will_fail = { repeat_mutate_stack_pop_all ~ "FAIL" }
stack_resume_after_fail = { will_fail | repeat_mutate_stack_pop_all }
peek_ = { PUSH(range) ~ PUSH(range) ~ PEEK ~ PEEK }
peek_all = { PUSH(range) ~ PUSH(range) ~ PEEK_ALL }
peek_slice_23 = { PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PEEK[1..-2] }
pop_ = { PUSH(range) ~ PUSH(range) ~ POP ~ POP }
pop_all = { PUSH(range) ~ PUSH(range) ~ POP_ALL }
pop_fail = { PUSH(range) ~ !POP ~ range ~ POP }
checkpoint_restore = ${
PUSH("") ~ (PUSH("a") ~ "b" ~ POP | DROP ~ "b" | POP ~ "a") ~ EOI
string = { "abc" }
insensitive = { ^"abc" }
range = { '0'..'9' }
ident = { string }
pos_pred = { &string }
neg_pred = { !string }
double_neg_pred = { !!string }
sequence = !{ string ~ string }
sequence_compound = ${ string ~ string }
sequence_atomic = @{ string ~ string }
sequence_non_atomic = @{ sequence }
sequence_atomic_compound = @{ sequence_compound }
sequence_nested = { string ~ string }
sequence_compound_nested = ${ sequence_nested }
node_tag = { #string = string }
choice = { string | range }
choice_prefix = { | string | range }
optional = { string? }
repeat = { string* }
repeat_atomic = @{ string* }
repeat_once = { string+ }
repeat_once_atomic = @{ string+ }
repeat_min_max = { string{2, 3} }
repeat_min_max_atomic = @{ string{2, 3} }
repeat_exact = { string{2} }
repeat_min = { string{2, } }
repeat_min_atomic = @{ string{2, } }
repeat_max = { string{, 2} }
repeat_max_atomic = @{ string{, 2} }
soi_at_start = { SOI ~ string }
repeat_mutate_stack = { (PUSH('a'..'c') ~ ",")* ~ POP ~ POP ~ POP }
repeat_mutate_stack_pop_all = { (PUSH('a'..'c') ~ ",")* ~ POP_ALL }
will_fail = { repeat_mutate_stack_pop_all ~ "FAIL" }
stack_resume_after_fail = { will_fail | repeat_mutate_stack_pop_all }
peek_ = { PUSH(range) ~ PUSH(range) ~ PEEK ~ PEEK }
peek_all = { PUSH(range) ~ PUSH(range) ~ PEEK_ALL }
peek_slice_23 = { PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PUSH(range) ~ PEEK[1..-2] }
pop_ = { PUSH(range) ~ PUSH(range) ~ POP ~ POP }
pop_all = { PUSH(range) ~ PUSH(range) ~ POP_ALL }
pop_fail = { PUSH(range) ~ !POP ~ range ~ POP }
checkpoint_restore = ${
PUSH("") ~ (PUSH("a") ~ "b" ~ POP | DROP ~ "b" | POP ~ "a") ~ EOI
}
ascii_digits = { ASCII_DIGIT+ }
ascii_nonzero_digits = { ASCII_NONZERO_DIGIT+ }
ascii_bin_digits = { ASCII_BIN_DIGIT+ }
ascii_oct_digits = { ASCII_OCT_DIGIT+ }
ascii_hex_digits = { ASCII_HEX_DIGIT+ }
ascii_alpha_lowers = { ASCII_ALPHA_LOWER+ }
ascii_alpha_uppers = { ASCII_ALPHA_UPPER+ }
ascii_alphas = { ASCII_ALPHA+ }
ascii_alphanumerics = { ASCII_ALPHANUMERIC+ }
asciis = { ASCII+ }
newline = { NEWLINE+ }
unicode = { XID_START ~ XID_CONTINUE* }
SYMBOL = { "shadows builtin" }
ascii_digits = { ASCII_DIGIT+ }
ascii_nonzero_digits = { ASCII_NONZERO_DIGIT+ }
ascii_bin_digits = { ASCII_BIN_DIGIT+ }
ascii_oct_digits = { ASCII_OCT_DIGIT+ }
ascii_hex_digits = { ASCII_HEX_DIGIT+ }
ascii_alpha_lowers = { ASCII_ALPHA_LOWER+ }
ascii_alpha_uppers = { ASCII_ALPHA_UPPER+ }
ascii_alphas = { ASCII_ALPHA+ }
ascii_alphanumerics = { ASCII_ALPHANUMERIC+ }
asciis = { ASCII+ }
newline = { NEWLINE+ }
unicode = { XID_START ~ XID_CONTINUE* }
SYMBOL = { "shadows builtin" }

han = { HAN+ }
hangul = { HANGUL+ }
han = { HAN+ }
hangul = { HANGUL+ }
hiragana = { HIRAGANA+ }
arabic = { ARABIC+ }
emoji = { EMOJI+ }
arabic = { ARABIC+ }
emoji = { EMOJI+ }

WHITESPACE = _{ " " }
COMMENT = _{ "$"+ }
COMMENT = _{ "$"+ }

// Line comment

/* 1-line multiline comment */

/*
N-line multiline comment
*/
/* N-line multiline comment */

/*
// Line comment inside multiline
// Line comment inside multiline

/*
(Multiline inside) multiline
*/
/*
(Multiline inside) multiline
*/

Invalid segment of grammar below (repeated rule)
Invalid segment of grammar below (repeated rule)

WHITESPACE = _{ "hi" }
WHITESPACE = _{ "hi" }
*/
24 changes: 11 additions & 13 deletions tests/grammars/http.pest
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
http = { SOI ~ (delimiter | request)* ~ EOI}
http = { SOI ~ (delimiter | request)* ~ EOI }

request = {
request_line ~
headers? ~
NEWLINE
request = {
request_line ~ headers? ~ NEWLINE
}

request_line = _{ method ~ " "+ ~ uri ~ " "+ ~ "HTTP/" ~ version ~ NEWLINE }
uri = { (!whitespace ~ ANY)+ }
method = { ("GET" | "DELETE" | "POST" | "PUT") }
version = { (ASCII_DIGIT | ".")+ }
whitespace = _{ " " | "\t" }
uri = { (!whitespace ~ ANY)+ }
method = { ("GET" | "DELETE" | "POST" | "PUT") }
version = { (ASCII_DIGIT | ".")+ }
whitespace = _{ " " | "\t" }

headers = { header+ }
header = { header_name ~ ":" ~ whitespace ~ header_value ~ NEWLINE }
header_name = { (!(NEWLINE | ":") ~ ANY)+ }
headers = { header+ }
header = { header_name ~ ":" ~ whitespace ~ header_value ~ NEWLINE }
header_name = { (!(NEWLINE | ":") ~ ANY)+ }
header_value = { (!NEWLINE ~ ANY)+ }

delimiter = { NEWLINE+ }
delimiter = { NEWLINE+ }
7 changes: 3 additions & 4 deletions tests/grammars/json.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
Expand All @@ -19,10 +19,9 @@ pair = { string ~ ":" ~ value }

array = { "[" ~ value ~ ("," ~ value)* ~ "]" | "[" ~ "]" }


//////////////////////
/// /
/// Matches value, e.g.: `"foo"`, `42`, `true`, `null`, `[]`, `{}`.
//////////////////////
/// /
value = { string | number | object | array | bool | null }

string = @{ "\"" ~ inner ~ "\"" }
Expand Down
2 changes: 1 addition & 1 deletion tests/grammars/lists.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
Expand Down
15 changes: 7 additions & 8 deletions tests/grammars/reporting.pest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// pest. The Elegant Parser
// Copyright (c) 2018 Dragoș Tiselice
//
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
Expand All @@ -12,16 +12,15 @@ b = { "b" }
c = { "c" }
d = { ANY }

choices = _{ a | b | c }
choices_no_progress = { a | b | c }
choices_a_progress = { a ~ a | b | c }
choices_b_progress = { a | b ~ b | c }
choices = _{ a | b | c }
choices_no_progress = { a | b | c }
choices_a_progress = { a ~ a | b | c }
choices_b_progress = { a | b ~ b | c }

level1 = _{ level2 }
level2 = _{ a | b | c }

negative = _{ !d }
negative = _{ !d }
negative_match = _{ !a ~ b }
mixed = _{ !d | a }
mixed = _{ !d | a }
mixed_progress = _{ (!d | a | b) ~ a }

Loading
Loading