arr-ai / wbnf

ωBNF implementation
Apache License 2.0
7 stars 4 forks source link

Add 'pragma' rules to the language, the first being .import #33

Closed ghost closed 4 years ago

ghost commented 4 years ago

adds .import <path> which merges the loaded grammar into the currently being-read one.

example:

a.wbnf
a -> "A";
b.wbnf
b -> "B";
z.wbnf
.import examples/a.wbnf
.import examples/b.wbnf

loads the tree:

$ go run . test --input z.wbnf --tree
grammar
├── stmt ‣ pragma
│   └── import
│       ├── 0‣.import
│       └── path
│           ├── 8‣examples
│           ├── 16‣/
│           └── 17‣a.wbnf
├── stmt
│   └── prod
│       ├── IDENT
│       │   └── 0‣a
│       ├── 2‣->
│       ├── term ‣ ... ‣ named ‣ atom ‣ STR
│       │   └── 5‣`"A"`
│       └── 8‣;
├── stmt
│   └── prod
│       ├── IDENT
│       │   └── 0‣b
│       ├── 2‣->
│       ├── term ‣ ... ‣ named ‣ atom ‣ STR
│       │   └── 5‣`"B"`
│       └── 8‣;
└── stmt ‣ pragma
    └── import
        ├── 24‣.import
        └── path
            ├── 32‣examples
            ├── 40‣/
            └── 41‣b.wbnf

The .import will only actually do anything if the wbnf is loaded through wbnf.Compile which I think is reasonable.

Also some minor changes to the test command so you dnot have to provide a grammar if you just want to test a wbnf grammar