gen-text 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -3
- data/lib/gen_text/compile.rb +16 -6
- metadata +1 -1
data/README.md
CHANGED
@@ -24,15 +24,33 @@ Here `file.g` is a file containing the grammar description which consists of the
|
|
24
24
|
|
25
25
|
nonterminal3 = expr3 ;
|
26
26
|
|
27
|
-
Nonterminals start from a letter or "\_" and may contain alphanumeric characters, "\_", "-"
|
27
|
+
Nonterminals start from a letter or "\_" and may contain alphanumeric characters, "\_", "-" and ":".
|
28
28
|
|
29
29
|
You may also use backquoted nonterminals:
|
30
30
|
|
31
31
|
`nonterminal1` = expr1 ;
|
32
32
|
|
33
33
|
`Nonterminal with arbitrary characters: [:|:]\/!` = expr2 ;
|
34
|
+
|
35
|
+
Trailing ";" can be omitted:
|
36
|
+
|
37
|
+
nonterminal1 = expr1
|
38
|
+
|
39
|
+
nonterminal2 = expr2
|
40
|
+
|
41
|
+
nonterminal3 = expr3
|
42
|
+
|
43
|
+
Also you may omit the left part in the first rule:
|
44
|
+
|
45
|
+
expr1
|
34
46
|
|
35
|
-
|
47
|
+
nonterminal2 = expr2
|
48
|
+
|
49
|
+
nonterminal3 = expr3
|
50
|
+
|
51
|
+
### Expressions ###
|
52
|
+
|
53
|
+
You may use the following expressions in the rules' right part:
|
36
54
|
|
37
55
|
<table>
|
38
56
|
<thead>
|
@@ -158,6 +176,18 @@ You may use the following expressions in the right part:
|
|
158
176
|
|
159
177
|
TODO: Capture the generated output.
|
160
178
|
|
179
|
+
### Alternate syntax ###
|
180
|
+
|
181
|
+
You may use "<-" instead of "=":
|
182
|
+
|
183
|
+
nonterm1 <- expr1 ;
|
184
|
+
|
185
|
+
nonterm2 <- expr2 ;
|
186
|
+
|
187
|
+
and "/" instead of "|":
|
188
|
+
|
189
|
+
`simple choice` <- "a" / "b" ;
|
190
|
+
|
161
191
|
Examples
|
162
192
|
--------
|
163
193
|
|
@@ -166,5 +196,5 @@ See them in "sample" directory.
|
|
166
196
|
Links
|
167
197
|
-----
|
168
198
|
|
169
|
-
- [Documentation](http://www.rubydoc.info/gems/gen-text/0.0.
|
199
|
+
- [Documentation](http://www.rubydoc.info/gems/gen-text/0.0.3)
|
170
200
|
- [Source code](https://github.com/LavirtheWhiolet/gen-text)
|
data/lib/gen_text/compile.rb
CHANGED
@@ -44,7 +44,7 @@ module GenText
|
|
44
44
|
class Label
|
45
45
|
end
|
46
46
|
|
47
|
-
|
47
|
+
Grammar = ASTNode.new :first_expr, :rules do
|
48
48
|
|
49
49
|
def to_vm_code
|
50
50
|
rule_labels = {}; begin
|
@@ -55,7 +55,13 @@ module GenText
|
|
55
55
|
end
|
56
56
|
code =
|
57
57
|
[
|
58
|
-
|
58
|
+
*(
|
59
|
+
if first_expr then
|
60
|
+
first_expr.to_vm_code(Context.new(new_binding, rule_labels))
|
61
|
+
else
|
62
|
+
[[:call, rule_labels[rules.first.name]]]
|
63
|
+
end
|
64
|
+
),
|
59
65
|
[:halt],
|
60
66
|
*rules.map do |rule|
|
61
67
|
[
|
@@ -321,9 +327,13 @@ module GenText
|
|
321
327
|
# ---- Syntax ----
|
322
328
|
|
323
329
|
rule :start do
|
324
|
-
whitespace_and_comments and
|
330
|
+
whitespace_and_comments and grammar
|
331
|
+
end
|
332
|
+
|
333
|
+
rule :grammar do
|
334
|
+
first_expr = opt { e = expr and opt {semicolon} and e } and
|
325
335
|
rules = many { rule_definition } and
|
326
|
-
_(
|
336
|
+
_(Grammar[first_expr.first, rules])
|
327
337
|
end
|
328
338
|
|
329
339
|
rule :expr do
|
@@ -406,7 +416,7 @@ module GenText
|
|
406
416
|
_{ c = code("{=") and _(GenCode[c]) } or
|
407
417
|
_{ c = code("{?") and _(CheckCode[c]) } or
|
408
418
|
_{ action_code } or
|
409
|
-
_{ n = nonterm and not_follows(:eq) and _(RuleCall[n]) } or
|
419
|
+
_{ n = nonterm and not_follows(:eq, :larrow) and _(RuleCall[n]) } or
|
410
420
|
_{ gen_number } or
|
411
421
|
_{ lparen and e = expr and rparen and e }
|
412
422
|
end
|
@@ -422,7 +432,7 @@ module GenText
|
|
422
432
|
end
|
423
433
|
|
424
434
|
rule :rule_definition do
|
425
|
-
n = nonterm and (_{eq} or _{larrow}) and e = choice and semicolon and
|
435
|
+
n = nonterm and (_{eq} or _{larrow}) and e = choice and opt {semicolon} and
|
426
436
|
_(RuleDefinition[n, e])
|
427
437
|
end
|
428
438
|
|