Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pratt parsing and explicit node elision #4

Merged
merged 5 commits into from
Dec 23, 2024
Merged
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
446 changes: 310 additions & 136 deletions README.md

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions examples/c/src/c.llw
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ string_literal:
;
/// 6.5.1.1
generic_assoc_list:
generic_association (',' generic_association)* @
generic_association (',' generic_association)*
;
/// 6.5.1.1
generic_association:
Expand All @@ -70,15 +70,15 @@ postfix_expr:
| '--' @post_dec_expr
)
| ?1 '(' type_name ')' '{' [initializer_list [',']] '}' @compound_literal
| primary_expr
| primary_expr ^
;
/// 6.5.2
argument_expression_list:
assignment_expr (',' assignment_expr)* @
assignment_expr (',' assignment_expr)*
;
/// 6.5.3
unary_expr:
postfix_expr
postfix_expr ^
| '++' unary_expr @pre_inc_expr
| '--' unary_expr @pre_dec_expr
| '__extension__' cast_expr @extension_expr // GNU extension
Expand All @@ -93,7 +93,7 @@ unary_expr:
/// 6.5.4
cast_expr:
?1 '(' type_name ')' cast_expr
| unary_expr
| unary_expr ^
;
/// 6.5.5 - 6.5.14
bin_expr:
Expand All @@ -107,27 +107,27 @@ bin_expr:
| bin_expr '|' bin_expr
| bin_expr '&&' bin_expr
| bin_expr '||' bin_expr
| cast_expr
| cast_expr ^
;
/// 6.5.15 (empty second argument is GNU extension)
conditional_expr:
bin_expr ['?' [expr] ':' conditional_expr]
conditional_expr^:
bin_expr ['?' [expr] ':' conditional_expr >]
;
/// 6.5.16
assignment_expr:
conditional_expr [('=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|=') assignment_expr]
assignment_expr^:
conditional_expr [('=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|=') assignment_expr >]
;
/// 6.5.17
expr:
expr^:
comma_expr
;
/// 6.5.17
comma_expr:
comma_expr ',' assignment_expr
| assignment_expr
| assignment_expr ^
;
/// 6.6
constant_expr:
constant_expr^:
conditional_expr
;

Expand All @@ -145,11 +145,11 @@ declaration_specifiers:
;
/// 6.7
init_declarator_list:
init_declarator (',' gnu_attributes init_declarator)* @
init_declarator (',' gnu_attributes init_declarator)*
;
/// 6.7
init_declarator:
declarator [gnu_asm] gnu_attributes ['=' initializer] @
declarator [gnu_asm] gnu_attributes ['=' initializer]
;
/// 6.7.1
storage_class_specifier:
Expand Down Expand Up @@ -183,7 +183,7 @@ specifier_qualifier_list:
;
/// 6.7.2.1
struct_declarator_list:
struct_declarator (',' gnu_attributes struct_declarator)* @
struct_declarator (',' gnu_attributes struct_declarator)*
;
/// 6.7.2.1
struct_declarator:
Expand All @@ -202,7 +202,7 @@ typeof_specifier:
;
/// 6.7.2.2
enumerator_list:
enumerator (?1 ',' enumerator)* @
enumerator (?1 ',' enumerator)*
;
/// 6.7.2.2
enumerator:
Expand Down Expand Up @@ -250,15 +250,15 @@ parameter_type_list:
;
/// 6.7.6
parameter_list:
parameter_declaration (?1 ',' parameter_declaration)* @
parameter_declaration (?1 ',' parameter_declaration)*
;
/// 6.7.6
parameter_declaration:
declaration_specifiers [?1 declarator | abstract_declarator] gnu_attributes
;
/// 6.7.6
identifier_list:
Identifier (',' Identifier)* @
Identifier (',' Identifier)*
;
/// 6.7.7
type_name:
Expand Down Expand Up @@ -313,7 +313,7 @@ static_assert_declaration:
// A.2.3 Statements

/// 6.8
statement:
statement^:
?1 labeled_statement
| compound_statement
| expression_statement
Expand Down Expand Up @@ -341,7 +341,7 @@ compound_statement:
'{' #1 block_item* #2 '}'
;
/// 6.8.2
block_item:
block_item^:
?1 declaration | statement
;
/// 6.8.3
Expand Down Expand Up @@ -422,14 +422,14 @@ declaration_list:


// GNU attribute extension
gnu_attributes:
(?1 gnu_attribute)*
gnu_attributes^:
[?1 (?1 gnu_attribute)+ >]
;
gnu_attribute:
'__attribute__' '(' '(' attribute_list ')' ')'
;
attribute_list:
attrib (',' attrib)* @
attrib (',' attrib)*
;
attrib:
[attrib_name ['(' [?1 Identifier [',' constant_expr (',' constant_expr)*] | argument_expression_list] ')']]
Expand Down
2 changes: 1 addition & 1 deletion examples/c/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl<'a> PredicatesAndActions for Parser<'a> {
}
fn predicate_cast_expr_1(&self) -> bool {
if self.is_parenthesized_type() {
// check this is not a compund literal
// check this is not a compound literal
let mut it = self.cst.tokens[self.pos..].iter().skip(1);
let mut paren_depth = 1;
loop {
Expand Down
18 changes: 9 additions & 9 deletions examples/calc/src/calc.llw
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
token Num='<number>';
token Plus='+' Minus='-' Star='*' Slash='/';
token Plus='+' Minus='-' Star='*' Slash='/' Pow='^';
token LPar='(' RPar=')';
token Whitespace;

skip Whitespace;

right '^';
start calc;

calc: expr;
calc^: expr;
expr:
expr ('*' | '/') expr
| expr ('+' | '-') expr
| atomic
expr '^' expr @binary_expr
| expr ('*' | '/') expr @binary_expr
| expr ('+' | '-') expr @binary_expr
| ('-' | '+') expr @unary_expr
| Num @literal_expr
| '(' expr ')' @paren_expr
;
atomic: literal | paren;
literal: Num;
paren: '(' expr ')';
4 changes: 3 additions & 1 deletion examples/calc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Token {
Whitespace,
#[regex(r"[0-9]+(\.[0-9]+)?")]
Num,
#[token("^")]
Pow,
#[token("+")]
Plus,
#[token("-")]
Expand All @@ -49,7 +51,7 @@ pub enum Token {
LPar,
#[token(")")]
RPar,
#[regex(r"[^0-9 \t\n\f\+\-\*/\(\)]+", |_| false)]
#[regex(r"[^0-9 \t\n\f\+\-\*/\(\)^]+", |_| false)]
Error,
}

Expand Down
2 changes: 1 addition & 1 deletion examples/json/src/json.llw
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ skip Whitespace;
start file;

file: value;
value:
value^:
object
| array
| literal
Expand Down
25 changes: 9 additions & 16 deletions examples/l/src/l.llw
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@ param_list: '(' [param (?1 ',' param)* [',']] ')';
param: Name ':' type_expr;
type_expr: Name;
block: '{' stmt* '}';
stmt:
stmt^:
stmt_expr
| stmt_let
| block
| stmt_return
;
stmt_expr: expr ';';
stmt_let: 'let' Name '=' expr ';';
stmt_return: 'return' [expr] ';';
expr: expr_bin;
expr_bin:
expr_bin ('*' | '/') expr_bin
| expr_bin ('+' | '-') expr_bin
| expr_call
expr:
expr ('*' | '/') expr @expr_binary
| expr ('+' | '-') expr @expr_binary
| expr arg_list @expr_call
| Name @expr_name
| '(' expr ')' @expr_paren
| expr_literal ^
;
expr_call:
expr_call arg_list
| expr_literal
| expr_name
| expr_paren
;
arg_list: '(' [expr (?1 ',' expr)* [',']] ')';
expr_literal: Int | 'true' | 'false';
expr_name: Name;
expr_paren: '(' expr ')';
arg_list: '(' [expr (?1 ',' expr)* [',']] ')';
2 changes: 1 addition & 1 deletion examples/l/tests/data/incomplete.tree
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ File [0..42]
Whitespace " " [23..24]
Asn "=" [24..25]
Whitespace " " [25..26]
ExprBin [26..29]
ExprBinary [26..29]
ExprLiteral [26..27]
Int "2" [26..27]
Whitespace " " [27..28]
Expand Down
28 changes: 14 additions & 14 deletions examples/lua/src/lua.llw
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ start chunk;

chunk: block;
block: stat* [retstat];
stat:
stat^:
emptystat
| expstat
| label
Expand All @@ -36,7 +36,7 @@ stat:
;
emptystat: ';';
/// superset of official grammar, check in cst that syntax is valid
expstat: prefixexp [(',' prefixexp)* '=' explist @assignstat] @;
expstat: prefixexp [(',' prefixexp)* '=' explist @assignstat];

breakstat: 'break';
gotostat: 'goto' Name;
Expand All @@ -60,16 +60,16 @@ localstat:
| attnamelist ['=' explist]
)
;
attnamelist: Name attrib (',' Name attrib)*;
attrib: ['<' Name '>'];
attnamelist: Name [attrib] (',' Name [attrib])*;
attrib: '<' Name '>';
retstat: 'return' [explist] [';'];
label: '::' Name '::';
funcname: Name ('.' Name)* [':' Name];
namelist: Name (',' Name)* @;
explist: exp (',' exp)* @;
namelist: Name (',' Name)*;
explist: exp (',' exp)*;
args_explist: '(' [exp (',' exp)*] ')'; // extra rule for better error handling

exp: binexp;
exp^: binexp;
binexp:
binexp ('*' | '/' | '//' | '%') binexp
| binexp ('+' | '-') binexp
Expand All @@ -81,14 +81,14 @@ binexp:
| binexp ('<' | '>' | '<=' | '>=' | '~=' | '==') binexp
| binexp 'and' binexp
| binexp 'or' binexp
| unaryexp
| unaryexp ^
;
unaryexp:
('not' | '-' | '#' | '~') unaryexp
| powexp
| powexp ^
;
powexp:
(literalexp | prefixexp | tableconstructor | functiondef) ['^' unaryexp]
powexp^:
(literalexp | prefixexp | tableconstructor | functiondef) ['^' unaryexp >]
;
literalexp:
'nil'
Expand All @@ -104,10 +104,10 @@ prefixexp:
prefixexp '[' exp ']' @indexexp
| prefixexp '.' Name @fieldexp
| ?1 prefixexp (args | ':' Name args) @callexp // resolve ambiguity described in section 3.3.1
| nameexp
| parenexp
| nameexp ^
| parenexp ^
;
args:
args^:
args_explist
| tableconstructor
| stringarg
Expand Down
16 changes: 8 additions & 8 deletions examples/oberon0/src/oberon0.llw
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ skip Comment Whitespace;
start module;

selector:
('.' Ident | '[' expression ']')*
('.' Ident | '[' expression ']')+
;
factor:
Ident selector | Number | '(' expression ')' | '~' factor
Ident [selector] | Number | '(' expression ')' | '~' factor
;
term:
factor (('*' | 'DIV' | 'MOD' | '&') factor)*
term^:
factor [(('*' | 'DIV' | 'MOD' | '&') factor)+ >]
;
simple_expression:
['+' | '-'] term (('+' | '-' | 'OR') term)*
;
expression:
simple_expression [('=' | '#' | '<' | '<=' | '>' | '>=') simple_expression]
expression^:
simple_expression [('=' | '#' | '<' | '<=' | '>' | '>=') simple_expression >]
;
assignment_or_procedure_call:
Ident selector (':=' expression @assignment | [actual_parameters] @procedure_call)
Ident [selector] (':=' expression @assignment | [actual_parameters] @procedure_call)
;
actual_parameters: '(' [expression (',' expression)*] ')';
if_statement:
Expand All @@ -44,7 +44,7 @@ while_statement:
repeat_statement:
'REPEAT' statement_sequence 'UNTIL' expression
;
statement:
statement^:
[assignment_or_procedure_call | if_statement | while_statement | repeat_statement]
;
statement_sequence: statement (';' statement)*;
Expand Down
2 changes: 1 addition & 1 deletion examples/oberon0/tests/data/incomplete.diag
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: invalid syntax, expected one of: '+', '&', ',', 'DIV', 'DO', 'ELSE', 'ELSIF', 'END', '=', '>=', '>', '#', '<=', '<', 'MOD', '*', 'OF', 'OR', ']', ')', ';', '-', 'THEN', 'UNTIL'
error: invalid syntax, expected one of: '+', '&', ',', 'DIV', 'DO', '.', 'ELSE', 'ELSIF', 'END', '=', '>=', '>', '#', '[', '<=', '<', 'MOD', '*', 'OF', 'OR', ']', ')', ';', '-', 'THEN', 'UNTIL'
┌─ <input>:6:7
6 │ x := 2 +
Expand Down
Loading
Loading