Skip to content

Commit e4ab5c9

Browse files
authored
Write a grammar! (#5)
I may be able to nerdsnipe @jackdotink into writing a parser, but in exchange, I had to provide a grammar. So, here is an initial attempt at a grammar, which will probably need a bunch of revisions.
1 parent 6ee4899 commit e4ab5c9

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "sanguinello"
33
version = "0.0.0"
4-
authors = ["aaron weiss <aweiss@hey.com>"]
4+
authors = ["ariel weiss <aweiss@hey.com>"]
55
license = "BSD-2-Clause"
66
edition = "2021"
77

GRAMMAR.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
```
2+
binding = NAME [':' type]
3+
bindings = binding {',' binding}
4+
5+
typebinding = 'type' NAME ['<' generictypeswithdefaults '>'] '=' type
6+
7+
path = '@' [NAME]
8+
| path '/' NAME
9+
import = 'import' binding '=' path
10+
export = 'export' bindings '=' exprs
11+
12+
-- we could just pick one, i guess?
13+
fnword = 'fn' | 'function'
14+
15+
block = {stat [';'] NEWLINE} [expr]
16+
stat = var '=' expr
17+
| var compoundop expr
18+
| 'while' expr 'do' block 'end'
19+
| 'repeat' block 'until' expr
20+
| 'for' bindings 'in' exprs 'do' block 'end'
21+
| fnword funcname ['<' generictypes '>'] '(' [parameters] ')' [ ':' type] block 'end'
22+
| 'module' binding 'do' properties 'end'
23+
| 'local' bindings '=' exprs
24+
| ['export'] typebinding
25+
| import | export
26+
| expr
27+
| 'break' | 'continue'
28+
| 'return' [expr]
29+
30+
varroot = NAME | '(' expr ')'
31+
varsuffix = [':' NAME] '(' [exprs] ')' | '.' NAME | '[' expr ']'
32+
var = varroot {varsuffix}
33+
vars = var {',' var}
34+
35+
expr = unop expr { binop expr }
36+
| 'do' block 'end'
37+
| 'if' expr 'then' block {'elseif' expr 'then' block} ['else' block] 'end'
38+
| fnword ['<' generictypes '>'] '(' [parameters] ')' [ ':' type] block 'end'
39+
| simpleexpr '::' type
40+
| simpleexpr
41+
simpleexpr = var
42+
| literal
43+
exprs = expr {separator expr} [separator]
44+
separator = ',' | ';'
45+
46+
literal = NUMBER
47+
| STRING
48+
| 'nil'
49+
| 'true'
50+
| 'false'
51+
| table
52+
| array
53+
literals = literal {',' literal}
54+
55+
array = '[' [exprs] ']'
56+
table = '{' [properties] '}'
57+
properties = property {separator property} [separator]
58+
property = '[' expr ']' '=' expr
59+
| NAME '=' expr
60+
| typebinding
61+
62+
compoundop :: '+=' | '-=' | '*=' | '/=' | '//=' | '%=' | '^=' | '..='
63+
binop = '+' | '-' | '*' | '/' | '//' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'
64+
unop = '-' | 'not' | '#'
65+
66+
type = [simpletype {'?'}] {'|' simpletype {'?'}}
67+
| [simpletype {'?'}] {'&' simpletype {'?'}}
68+
types = type {',' type}
69+
70+
generictype = NAME
71+
generictypes = generictype {',' generictype}
72+
generictypewithdefault = generictype ['=' type]
73+
generictypeswithdefaults = generictypewithdefault {',' generictypeswithdefaults}
74+
75+
tabletype = '{' [proplist] '}'
76+
propertytypes = propertytypeorindexer {separator propertytypeorindexer} [separator]
77+
propertytype = NAME ':' type
78+
indexer = '[' type ']' ':' type
79+
propertytypeorindexer = ['read' | 'write'] (propertytype | indexer)
80+
| 'type' NAME ['<' generictypes '>']
81+
proplist = propertytypeorindexer {',' propertytypeorindexer}
82+
83+
singleton = STRING | 'true' | 'false'
84+
simpletype = 'nil'
85+
| singleton
86+
| 'boolean'
87+
| 'string'
88+
| 'number'
89+
| '(' types ')'
90+
| ['<' generictypes '>'] '(' types ')' '->' type
91+
| tabletype
92+
| '[' type ']'
93+
```

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 2-Clause License
22

3-
Copyright (c) 2023, aaron weiss <aweiss@hey.com>
3+
Copyright (c) 2023-2025, ariel weiss <aweiss@hey.com>
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:

0 commit comments

Comments
 (0)