File: usedir.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (38 lines) | stat: -rw-r--r-- 922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# re2py $INPUT -o $OUTPUT

# This example shows how to combine reusable re2c blocks: two blocks
# ('colors' and 'fish') are merged into one. The 'salmon' rule occurs
# in both blocks; the 'fish' block takes priority because it is used
# earlier. Default rule * occurs in all three blocks; the local (not
# inherited) definition takes priority.

from enum import Enum

class Ans(Enum):
    COLOR = 1
    FISH = 2
    DUNNO = 3

%{rules:colors
    *                            { raise "ah" }
    "red" | "salmon" | "magenta" { return Ans.COLOR }
%}

%{rules:fish
    *                            { raise "oh" }
    "haddock" | "salmon" | "eel" { return Ans.FISH }
%}

def lex(yyinput):
    yycursor = 0
%{
    re2c:yyfill:enable = 0;
    re2c:indent:top = 1;

    !use:fish;
    !use:colors;
    * { return Ans.DUNNO } // overrides inherited '*' rules
%}

assert lex(b"salmon") == Ans.FISH
assert lex(b"what?") == Ans.DUNNO