Skip to content

Commit

Permalink
Write a grammar! (#5)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aatxe authored Feb 1, 2025
1 parent 6ee4899 commit e4ab5c9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sanguinello"
version = "0.0.0"
authors = ["aaron weiss <aweiss@hey.com>"]
authors = ["ariel weiss <aweiss@hey.com>"]
license = "BSD-2-Clause"
edition = "2021"

Expand Down
93 changes: 93 additions & 0 deletions GRAMMAR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
```
binding = NAME [':' type]
bindings = binding {',' binding}
typebinding = 'type' NAME ['<' generictypeswithdefaults '>'] '=' type
path = '@' [NAME]
| path '/' NAME
import = 'import' binding '=' path
export = 'export' bindings '=' exprs
-- we could just pick one, i guess?
fnword = 'fn' | 'function'
block = {stat [';'] NEWLINE} [expr]
stat = var '=' expr
| var compoundop expr
| 'while' expr 'do' block 'end'
| 'repeat' block 'until' expr
| 'for' bindings 'in' exprs 'do' block 'end'
| fnword funcname ['<' generictypes '>'] '(' [parameters] ')' [ ':' type] block 'end'
| 'module' binding 'do' properties 'end'
| 'local' bindings '=' exprs
| ['export'] typebinding
| import | export
| expr
| 'break' | 'continue'
| 'return' [expr]
varroot = NAME | '(' expr ')'
varsuffix = [':' NAME] '(' [exprs] ')' | '.' NAME | '[' expr ']'
var = varroot {varsuffix}
vars = var {',' var}
expr = unop expr { binop expr }
| 'do' block 'end'
| 'if' expr 'then' block {'elseif' expr 'then' block} ['else' block] 'end'
| fnword ['<' generictypes '>'] '(' [parameters] ')' [ ':' type] block 'end'
| simpleexpr '::' type
| simpleexpr
simpleexpr = var
| literal
exprs = expr {separator expr} [separator]
separator = ',' | ';'
literal = NUMBER
| STRING
| 'nil'
| 'true'
| 'false'
| table
| array
literals = literal {',' literal}
array = '[' [exprs] ']'
table = '{' [properties] '}'
properties = property {separator property} [separator]
property = '[' expr ']' '=' expr
| NAME '=' expr
| typebinding
compoundop :: '+=' | '-=' | '*=' | '/=' | '//=' | '%=' | '^=' | '..='
binop = '+' | '-' | '*' | '/' | '//' | '^' | '%' | '..' | '<' | '<=' | '>' | '>=' | '==' | '~=' | 'and' | 'or'
unop = '-' | 'not' | '#'
type = [simpletype {'?'}] {'|' simpletype {'?'}}
| [simpletype {'?'}] {'&' simpletype {'?'}}
types = type {',' type}
generictype = NAME
generictypes = generictype {',' generictype}
generictypewithdefault = generictype ['=' type]
generictypeswithdefaults = generictypewithdefault {',' generictypeswithdefaults}
tabletype = '{' [proplist] '}'
propertytypes = propertytypeorindexer {separator propertytypeorindexer} [separator]
propertytype = NAME ':' type
indexer = '[' type ']' ':' type
propertytypeorindexer = ['read' | 'write'] (propertytype | indexer)
| 'type' NAME ['<' generictypes '>']
proplist = propertytypeorindexer {',' propertytypeorindexer}
singleton = STRING | 'true' | 'false'
simpletype = 'nil'
| singleton
| 'boolean'
| 'string'
| 'number'
| '(' types ')'
| ['<' generictypes '>'] '(' types ')' '->' type
| tabletype
| '[' type ']'
```
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 2-Clause License

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

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

0 comments on commit e4ab5c9

Please sign in to comment.