sbnf 0.6.2

A BNF-style language for writing sublime-syntax files
Documentation
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# SBNF

[![Build Status](https://github.com/BenjaminSchaaf/sbnf/actions/workflows/rust.yml/badge.svg)](https://github.com/BenjaminSchaaf/sbnf/actions) [![Crate](https://badgen.net/crates/v/sbnf)](https://crates.io/crates/sbnf)

A BNF-style language for writing sublime-syntax files.

Try it out now on the [Live Playground!](https://benjaminschaaf.github.io/sbnf/playground.html)

SBNF is currently used for
[SWI-Prolog](https://github.com/BenjaminSchaaf/swi-prolog-sublime-syntax).

## Motivation & Goals

Writing syntax definitions is error prone and the result is hard to maintain.
The addition of `branch_point`, while a great feature, dramatically increases
complexity and duplication when used.

SBNF attempts do the following:
* Provide a maintainable, declarative language for writing sublime syntax
  definitions
* Compile quickly for fast iteration
* Compile to an efficient syntax, comparable to hand-made ones

## Installation

With [rust installed](https://www.rust-lang.org/tools/install) you can download,
build and install the latest released version of SBNF using:

```bash
$ cargo install sbnfc
```

Or if you want the latest features, clone this repository, then build and
install using:

```bash
$ cargo install --path cli
```

Note that in order to use the generated syntax you'll need at minimum Sublime
Text build 4077 with support for version 2 of Sublime Syntax.

### Sublime Syntax

The syntax definition for SBNF is found in `sbnf/sbnf.sbnf`. To compile it
simply run `sbnf sbnf/sbnf.sbnf`, you can then symlink or copy the `sbnf/`
directory to your user packages.

## Example

The following is a sbnf grammar for a cut-down version of C. It only allows
global/local variable declarations, function definitions and simple function
calls. Even this cut down version is extremely difficult to parse correctly with
the required `meta.function` and `meta.function-call` scopes, as both function
definitions and function calls require branch points.

```sbnf
NAME = `simplec`

prototype : ( ~comment )* ;

comment : '(//+).*\n?'{comment.line, 1: punctuation.definition.comment} ;

main : ( variable-declaration | function-definition )* ;

IDENTIFIER = '\b[A-Za-z_]+\b'

function-definition{meta.function}
: type
  IDENTIFIER{entity.name.function}
  `(`
  `)`
  block
;

block{meta.block} : '{' statement* '}' ;

statement : variable-declaration
          | value ';'
          | block
          ;

variable-declaration : type IDENTIFIER{variable} ( '=' value )? ';' ;

type : IDENTIFIER{storage.type} ;

value : '[0-9]+'{constant.numeric}
      | function-call
      ;

# Function calls don't have arguments :)
function-call{meta.function-call}
: IDENTIFIER{variable.function meta.path} `(` `)` ;
```

The above grammar compiles to the following:

```yaml
%YAML 1.2
---
# http://www.sublimetext.com/docs/syntax.html
version: 2
name: simplec
scope: source.simplec
contexts:
  # Rule: block
  block|0:
    - meta_content_scope: meta.block.simplec
    - match: '{'
      scope: meta.block.simplec
      set: block|1
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: block
  block|1:
    - meta_content_scope: meta.block.simplec
    - include: include!block@1
    - match: '[0-9]+'
      scope: meta.block.simplec constant.numeric.simplec
      push: [block|meta, statement|0]
    - match: '{'
      scope: meta.block.simplec meta.block.simplec
      push: [block|meta, block|1]
    - match: '}'
      scope: meta.block.simplec
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: block
  #  For branch point 'block@1'
  block|2|block@1:
    - match: '\b[A-Za-z_]+\b'
      scope: meta.block.simplec variable.simplec
      set: [block|meta, variable-declaration|2]
    - match: '\S'
      fail: block@1
  # Rule: block
  #  For branch point 'block@1'
  block|3|block@1:
    - match: '\('
      scope: meta.block.simplec meta.function-call.simplec
      set: [block|meta, statement|0, function-call|1]
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Meta scope context for block
  block|meta:
    - meta_content_scope: meta.block.simplec
    - match: ''
      pop: true
  # Rule: function-call
  function-call|0:
    - meta_content_scope: meta.function-call.simplec
    - match: '\('
      scope: meta.function-call.simplec
      set: function-call|1
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: function-call
  function-call|1:
    - meta_content_scope: meta.function-call.simplec
    - match: '\)'
      scope: meta.function-call.simplec
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  function-call|2|block@1:
    - meta_include_prototype: false
    - match: '\b[A-Za-z_]+\b'
      scope: meta.function-call.simplec variable.function.simplec meta.path.simplec
      push: block|3|block@1
      pop: true
  # Rule: function-definition
  function-definition|0:
    - meta_content_scope: meta.function.simplec
    - match: '\)'
      scope: meta.function.simplec
      set: [function-definition|meta, block|0]
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Meta scope context for function-definition
  function-definition|meta:
    - meta_content_scope: meta.function.simplec
    - match: ''
      pop: true
  # Include context for branch point block@1
  include!block@1:
    - match: '(?=\b[A-Za-z_]+\b)'
      branch_point: block@1
      branch:
        - type|2|block@1
        - function-call|2|block@1
  # Include context for branch point main@1
  include!main@1:
    - match: '(?=\b[A-Za-z_]+\b)'
      branch_point: main@1
      branch:
        - type|0|main@1
        - type|1|main@1
  # Rule: main
  main:
    - include: include!main@1
    - match: '\S'
      scope: invalid.illegal.simplec
  # Rule: main
  #  For branch point 'main@1'
  main|0|main@1:
    - match: '\b[A-Za-z_]+\b'
      scope: variable.simplec
      push: main|2|main@1
      pop: true
    - match: '\S'
      fail: main@1
  # Rule: main
  #  For branch point 'main@1'
  main|1|main@1:
    - match: '\b[A-Za-z_]+\b'
      scope: meta.function.simplec entity.name.function.simplec
      push: main|3|main@1
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: main
  #  For branch point 'main@1'
  main|2|main@1:
    - match: '='
      set: variable-declaration|0
    - match: ';'
      pop: true
    - match: '\S'
      fail: main@1
  # Rule: main
  #  For branch point 'main@1'
  main|3|main@1:
    - match: '\('
      scope: meta.function.simplec
      set: function-definition|0
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: prototype
  prototype:
    - match: '(//+).*\n?'
      scope: comment.line.simplec
      captures:
        1: punctuation.definition.comment.simplec
  # Rule: statement
  statement|0:
    - match: ';'
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  type|0|main@1:
    - meta_include_prototype: false
    - match: '\b[A-Za-z_]+\b'
      scope: storage.type.simplec
      push: main|0|main@1
      pop: true
  type|1|main@1:
    - meta_include_prototype: false
    - match: '\b[A-Za-z_]+\b'
      scope: meta.function.simplec storage.type.simplec
      push: main|1|main@1
      pop: true
  type|2|block@1:
    - meta_include_prototype: false
    - match: '\b[A-Za-z_]+\b'
      scope: storage.type.simplec
      push: block|2|block@1
      pop: true
  # Rule: variable-declaration
  variable-declaration|0:
    - match: '[0-9]+'
      scope: constant.numeric.simplec
      set: variable-declaration|1
    - match: '\b[A-Za-z_]+\b'
      scope: meta.function-call.simplec variable.function.simplec meta.path.simplec
      set: [variable-declaration|1, function-call|0]
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: variable-declaration
  variable-declaration|1:
    - match: ';'
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
  # Rule: variable-declaration
  variable-declaration|2:
    - match: '='
      set: variable-declaration|0
    - match: ';'
      pop: true
    - match: '\S'
      scope: invalid.illegal.simplec
      pop: true
```

## Usage

A SBNF file contains two types of elements: clauses and rules. Clauses provide
meta-data for the syntax such as the file extensions, as well as some
meta-programming. Rules are the bnf-style rules that define the parsing and
scoping of the grammar.

Comments in SBNF start with a `#` and end at the next newline.

See `sbnf.sbnf` for a full example grammar.

### Clauses

Clauses are in the form `<name> <parameters> = <value>`. The `name` must follow
[SCREAMING_SNAKE_CASE](https://en.wikipedia.org/wiki/SCREAMING_SNAKE_CASE). The
following names are reserved for meta-data:

* `NAME`: The name of the syntax. This defaults to the base-name of the sbnf
  file.
* `EXTENSIONS`: A space-separated list of file extensions. Equivalent to
  `file_extensions` in sublime-syntax.
* `FIRST_LINE`: A regex for matching the first line of a file. Equivalent to
  `first_line_match` in sublime-syntax.
* `SCOPE`: The default scope for the grammar. This defaults to `source.`
  followed by the lowercased name of the syntax.
* `SCOPE_POSTFIX`: A postfix appended to all scopes in the grammar (excluding
  the `SCOPE` clause). This defaults to the name lowercased. Can be left empty
  to leave out the postfix.
* `HIDDEN`: Whether the syntax will be shown in the menu in Sublime Text.

Example:

```sbnf
NAME = `SBNF`
EXTENSIONS = `sbnf`
# Don't need this, as this is already the default
# SCOPE = `source.sbnf`
```

### Rules

Rules are in the form `<name> <parameters> <options> : <expression> ;`. The
`name` must follow [kebab-case](https://en.wikipedia.org/wiki/Kebab_case).

Like sublime-syntax files, SBNF grammars have two entry points: `main`,
`prototype`. They behave identically to those in sublime-syntax files. Only
rules used directly or indirectly from an entry point are compiled.

Rules can optionally have parameters and options. Parameters are used for
meta-programming and options are used for sublime-syntax specific options.

Examples:

```sbnf
a : 'a' ;
b{source.b} : 'b' ;
c[S] : 'c'{#[S]} ;
d[S]{text.d} : a b c[S] ;
```

#### Expressions

Expressions may take any of the following forms:

* `` `<literal>` <options>``: A terminal matching text literally.
* `'<regex>' <options>`: A terminal matching text according to a regex.
* `<identifier> <arguments>`: A non-terminal matching another rule.
* `<expr> | <expr>`: An alternation of expressions. The grammar matches either
  the left or right expression. This can be used as a list, eg:
  `'a' | 'b' | 'c'`.
* `<expr> <expr>`: A concatenation of expressions. The grammar matches the left
  expression followed by the right expression. This can be used as a list, eg:
  `'a' 'b' 'c'`.
* `(<expr>)`: A grouping.
* `<expr>?`: An optional expression. The grammar matches nothing or the
  expression.
* `<expr>*`: A repeating expression. The grammar matches the expression any
  number of times, including 0.
* `~<expr>`: A passive expression. The grammar matches *any text* until the
  expression matches.

#### Options

Options come in the following form: `{<param>, <key>: <value>}`. `<param>`,
`<key>` or `<value>` may contain any text except `,`, `:` or `}`. There may be
any number of options given, as allowed by whatever the options are for.
When there are no options the `{}` are optional.

The following options are allowed for rules:

* `<meta-scope>`: The meta-scope of the rule. Equivalent to `meta_scope` or
  `meta_content_scope` in sublime-syntax.

Literal and regex terminals are allowed the following arguments:

* `<scope>`: The scope of the terminal.
* `<capture>: <scope>`: The scope for a regex capture group. `<capture>` must be
  an integer.

#### Parameters

Parameters for rules and clauses take the form: `[<value>, <value>]`. `<value>`
may be either a regex terminal, a literal terminal or an identifier. The same
name may be used for rules/clauses with different sets of parameters.

A rule with parameters is instantiated when it is used. Matching is based on the
type and value of each parameter. Terminal arguments are matched based on regex
equivalence, while rule arguments are matched by name.

An identifier that does not reference a rule is a free variable unique to the
rule's scope. It matches any argument and may be passed in and or interpolated.

A variable may be interpolated using the following syntax: `#[]`. This can be
done inside any terminal or inside options.

Examples:

```sbnf
main
: a['a'] # instantiates rule 1
| a[a]   # instantiates rule 2
| a['b'] # instantiates rule 3
| b['b'] # error: Ambiguous instantiation
;

# Rule 1.
a['a'] : 'a' ;

# Rule 2.
a[a] : 'a' ;

# Rule 3.
a[A] : 'a' ;

b[A] : 'a' ;
b[B] : 'b' ;
```

There also exists a set of global arguments which are passed in from the command
line. These arguments are in the same form as other arguments and should be put
at the top of the file. They may only consist of variables and are available
globally, including for clauses.

Examples:

```sbnf
# Declares a single global parameter
[TYPE]

# Can be used in clauses
NAME = 'd-#[TYPE]'

# As well as rules
main : '#[TYPE]' ;
```

```bash
# 'dmd' is passed to TYPE when compiled
$ sbnf syntax.sbnf dmd
```

#### Include/Embed

SBNF also has support for including/embedding other sublime syntaxes. This can
only be done on a literal or regex terminal expression with a postfix of
`%include[<with_prototype>]{<syntax>}` for including a syntax or
`%embed[<regex>]{<syntax>}` for an embed.

Note that these translate directly to the sublime syntax include/embed
functionality and thus have the same limitations.

Examples:

```sbnf
# This is a basic implementation of the html script tag embedding the javascript
# syntax.
script
: '<script>'{tag.begin.script}
  %embed['</script>']{scope:source.js, embedded.js, 0: tag.end.script}
;
```

```yaml
# The above translates to the following context
script:
  - match: '<script>'
    scope: tag.begin.script.example
    embed: scope:source.js
    embed_scope: embedded.js.example
    escape: '</script>'
    escape_captures:
      0: tag.end.script.example
    pop: true
  - match: '\S'
    scope: invalid.illegal.example
```

```sbnf
# This is a basic implementation of a regex string. It has a prototype rule that
# extends the regex syntax with an escape sequence for the string.

regex-prototype{include-prototype: false}
: ( ~`\'`{constant.character.escape} )*
  # A lookahead is required here, as otherwise we would only pop one context
  # The same is required in a sublime-syntax file
  ~'(?=\')'
;

regex-string{string.quoted}
: `'`{punctuation.definition.string.begin}
  %include[regex-prototype]{scope:source.regexp}
  `'`{punctuation.definition.string.end}
;
```

```yaml
# The above translates to the following contexts
regex-string:
  - meta_content_scope: string.quoted.example
  - match: ''''
    scope: string.quoted.example punctuation.definition.string.begin.example
    set: [regex-string|0, regex-string|1]
  - match: '\S'
    scope: invalid.illegal.example
regex-string|0:
  - meta_content_scope: string.quoted.example
  - match: ''''
    scope: string.quoted.example punctuation.definition.string.end.example
    pop: true
  - match: '\S'
    scope: invalid.illegal.example
    pop: true
regex-string|1:
  - meta_include_prototype: false
  - match: ''
    set: scope:source.regexp
    with_prototype:
      - include: regex-prototype|0
regex-prototype|0:
  - meta_include_prototype: false
  - match: '\\'''
    scope: constant.character.escape.example
  - match: '(?='')'
    pop: true
```

### Command Line

```bash
$ sbnf --help
SBNF compiler 0.4.0

USAGE:
    sbnf [FLAGS] [OPTIONS] <INPUT> [ARGS]...

FLAGS:
    -g               Compile with debug scopes
    -h, --help       Prints help information
    -q               Do not display warnings
    -V, --version    Prints version information

OPTIONS:
    -o <output>        The file to write the compiled sublime-syntax to. Defaults to $INPUT.sublime-syntax if left out. Use a single dash `-` to write to stdout instead.

ARGS:
    <INPUT>      The SBNF file to compile
    <ARGS>...    Arguments to pass to the main and prototype rules
```

## Limitations

### Regex Equivalence

When determining whether to create a branch point in the sublime-syntax, SBNF
has to consider whether regexes overlap. Take the following example:

```sbnf
main : 'aa?'{scope1} 'b'
     | 'a'{scope2} 'c'
     ;
```

The regexes `'aa?'` and `'a'` both match `a`, meaning a branch point would be
required to correctly parse this syntax. SBNF *does not* create a branch point
here. Due to the complexities of regex, a branch point is only created with
equivalent regexes. Rewriting the example to work as expected with SBNF yields
the following:

```sbnf
main : 'aa'{scope1} 'b'
     | 'a'{scope1} 'b'
     | 'a'{scope2} 'c'
     ;
```

This is unlikely to change in the future, as SBNF does not make any attempt to
understand any regexes.

## TODO

* Fix known edge cases in compiler. In a couple places we panic!() instead of
  providing an implementation.
* Add warnings for when branches are used in non-popping loops.
* Fix infinite loop/recursion when rule refers to itself