Skip to content

Commit fade593

Browse files
committed
[Grammar] Prototype of the grammar of Oak with advanced syntax and capabilities, not yet implemented. Related to #42.
1 parent f942764 commit fade593

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/bootstrap/oak_grammar.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2016 Pierre Talbot (IRCAM)
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// WARNING: This is not a current valid Oak grammar. This is design experimentation on the grammar's evolution.
16+
17+
pub use self::oak::*;
18+
19+
grammar! oak {
20+
type Stream = RustTokens;
21+
type Context = FGrammar;
22+
type ErrorContext = SyntaxErrorContext;
23+
24+
extern .alloc_expr -> usize;
25+
extern .warning -> ();
26+
27+
grammar = block*
28+
29+
block = rs_inner_attrs item
30+
31+
rs_inner_attrs -> ()
32+
= ::inner_attributes > .push_attributes
33+
34+
item -> ()
35+
= ::parse_item > .push_rust_item
36+
/ rule
37+
38+
rule -> ()
39+
= .. ::outer_attributes rule_decl "=" rule_rhs > .push_rule
40+
41+
rule_decl -> SpannedIdent
42+
= .. ::ident > respan !> .current_rule
43+
44+
rule_rhs = .. choice > .alloc_expr
45+
46+
choice
47+
= branch % "/" > Expression::Choice
48+
49+
branch
50+
= sequence !">" !"->"
51+
/ .. sequence ">" ::ident > SemanticAction > .alloc_expr
52+
/ .. sequence "->" ty > TypeAscription > .alloc_expr
53+
54+
ty
55+
= "()" > IType::unit
56+
/ "(^)" > IType::Invisible
57+
\ "" !> UnknownTypeAscription
58+
59+
sequence
60+
= syntactic_predicate !syntactic_predicate
61+
/ .. (syntactic_predicate+ > Sequence) > .alloc_expr
62+
\ .. !syntactic_predicate !> EmptyRuleBodyError
63+
64+
syntactic_predicate
65+
= .. "!" repeat > NotPredicate > .alloc_expr
66+
/ .. "&" repeat > AndPredicate > .alloc_expr
67+
\ .. ("!" / "&") !repeat !> SyntacticPredicateExpectExpr
68+
/ repeat
69+
70+
repeat
71+
= .. atom "*" > ZeroOrMore > .alloc_expr
72+
/ .. atom "+" > OneOrMore > .alloc_expr
73+
/ .. atom "?" > ZeroOrOne > .alloc_expr
74+
/ atom
75+
76+
atom
77+
= .. (::cooked_literal > StrLiteral) > .alloc_expr
78+
/ .. ("." > AnySingleChar) > .alloc_expr
79+
/ "(" rule_rhs ")"
80+
/ .. (!::parse_item !rule_lhs ::ident > NonTerminalSymbol) > .alloc_expr
81+
/ "[" char_class "]"
82+
83+
char_class =
84+
\ .. !"]" !> MissingStringLiteral
85+
/ .. ::cooked_literal ~> ::char_range_set > .alloc_expr
86+
}
87+
88+
grammar! char_class {
89+
type ErrorContext = SyntaxErrorContext;
90+
91+
char_range_set -> CharacterClassExpr
92+
= .. warning_both_sep "-"? char_range* "-"? > CharacterClassExpr::full_range
93+
94+
char_range -> CharacterInterval =
95+
\ "-" &. !> BadCharClassSeparator
96+
/ . "-" . > CharacterInterval::new
97+
/ !"-" . > CharacterInterval::single
98+
99+
warning_both_sep -> ()
100+
= &("-" char_range* "-") !> WarningCharClassDoubleSep !> .warning
101+
}

tests/grammars/combinators.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use self::combinators::*;
1616

1717
grammar! combinators {
1818
// #![debug_api]
19+
#![show_api]
1920

2021
str_literal = "return"
2122

0 commit comments

Comments
 (0)