Grammar railroad diagram
- Dominant language
- JavaScript
- Stars
- 3.7k
- Forks
- 233
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 4
Description
Using a custom parser that translates `dbml/parser.pegjs` grammar to an EBNF understood by https://www.bottlecaps.de/rr/ui and manually fixing small errors we can have nice railroad diagram (https://en.wikipedia.org/wiki/Syntax_diagram) to help show/understand the syntax.
Copy and paste the EBNF shown bellow at https://www.bottlecaps.de/rr/ui on the tab Edit Grammar then click on the tab View Diagram.
```
//To be viewd at https://www.bottlecaps.de/rr/ui
rules ::=
expr*
expr ::=
TableSyntax
| RefSyntax
| EnumSyntax
| TableGroupSyntax
| ProjectSyntax
| __
TableSyntax ::=
table sp+ schema_name? name alias_def? sp* TableSettings? _ "{" TableBody "}"
RefSyntax ::=
ref_long
| ref_short
EnumSyntax ::=
enum sp+ schema_name? name _ "{" EnumBody "}"
TableGroupSyntax ::=
table_group sp+ schema_name? name _ "{" _ table_group_body _ "}"
ProjectSyntax ::=
project ( __ name )? _ "{" _ ProjectBody _ "}"
__ ::=
( comment | whitespace )+
project ::=
"project"
name ::=
character+
| ( quote [^\"\n]+ quote )
_ ::=
( comment | whitespace )*
ProjectBody ::=
_ ProjectElement* _
ProjectElement ::=
( _ ObjectNoteElement _ )
| ( _ TableSyntax _ )
| ( _ RefSyntax _ )
| ( _ EnumSyntax _ )
| ( TableGroupSyntax _ )
| ( _ ProjectField _ )
ObjectNoteElement ::=
ObjectNote
| ( "note" _ "{" _ StringLiteral _ "}" )
ProjectField ::=
name _ ":" _ StringLiteral
StringLiteral ::=
( '"' DoubleStringCharacter* '"' )
| ( "'''" MultiLineStringCharacter* "'''" )
| ( "'" SingleStringCharacter* "'" )
table_group ::=
"TableGroup"
sp ::=
" "
schema_name ::=
name "."
table_group_body ::=
( schema_name? name __ )*
ref_long ::=
ref ( __ name )? _ "{" _ ref_body _ "}"
ref_short ::=
ref ( sp+ name )? sp* ":" sp* ref_body
ref ::=
"ref"
ref_body ::=
field_identifier sp+ relation sp+ field_identifier sp* RefSettings?
field_identifier ::=
( name "." name "." RefField )
| ( name "." RefField )
relation ::=
[>-<]
RefSettings ::=
"[" RefSetting ( Comma RefSetting )* "]"
RefField ::=
RefSingleField
| RefMultipleFields
RefSingleField ::=
name
RefMultipleFields ::=
"(" sp* RefSingleField ( sp* Comma sp* RefSingleField )* sp* ")"
Comma ::=
","
RefSetting ::=
( _ OnUpdate _ )
| ( _ OnDelete _ )
OnUpdate ::=
"update:" _ ( no_action | restrict | cascade | set_null | set_default )
OnDelete ::=
"delete:" _ ( no_action | restrict | cascade | set_null | set_default )
no_action ::=
"no action"
restrict ::=
"restrict"
cascade ::=
"cascade"
set_null ::=
"set null"
set_default ::=
"set default"
table ::=
"table"
alias_def ::=
sp+ "as" sp+ name
TableSettings ::=
"[" TableSetting ( Comma TableSetting )* "]"
TableBody ::=
_ Field+ _ TableElement* _
Field ::=
_ name sp+ schema_name? type ( sp+ constrain )* ( sp+ FieldSettings )? sp* comment? newline
TableElement ::=
( _ Indexes _ )
| ( _ ObjectNoteElement _ )
Indexes ::=
_ indexes _ "{" IndexesBody "}"
type ::=
type_name ( sp* "(" sp* expression sp* ")" )?
constrain ::=
unique
| pk
FieldSettings ::=
"[" FieldSetting ( Comma FieldSetting )* "]"
comment ::=
single_line_comment
| multi_lime_comment
newline ::=
"\r\n"
| "\n"
enum ::=
"enum"
EnumBody ::=
_ EnumValue+ _
EnumValue ::=
_ name sp* EnumSetting? sp* comment? newline
EnumSetting ::=
"[" _ ObjectNote _ "]"
ObjectNote ::=
"note:" _ StringLiteral
FieldSetting ::=
( _ "not null" _ )
| ( _ "null" _ )
| ( _ "primary key" _ )
| ( _ "pk" _ )
| ( _ "unique" _ )
| ( _ "increment" _ )
| ( _ ObjectNote _ )
| ( _ RefInline _ )
| ( _ Default _ )
TableSetting ::=
( _ ObjectNote _ )
| ( _ HeaderColor _ )
HeaderColor ::=
_ header_color ":" _ sharp hex_color _
RefInline ::=
"ref:" sp* relation sp+ inline_field_identifier
Default ::=
"default:" _ DefaultVal
indexes ::=
"indexes"
IndexesBody ::=
_ Index+ _
Index ::=
_ ( SingleIndexSyntax | CompositeIndexSyntax ) _
SingleIndexSyntax ::=
_ SingleIndex sp* IndexSettings?
CompositeIndexSyntax ::=
_ CompositeIndex sp* IndexSettings?
SingleIndex ::=
( name sp* )
| Expression
IndexSettings ::=
( "[" sp* pk sp* "]" )
| ( "[" IndexSetting ( Comma IndexSetting )* "]" )
CompositeIndex ::=
"(" sp* SingleIndex ( Comma sp* SingleIndex )* ")"
Expression ::=
"`" [^`]* "`"
pk ::=
"pk"
IndexSetting ::=
( _ "unique" _ )
| ( _ IndexName _ )
| ( _ IndexType _ )
| ( _ ObjectNote _ )
IndexName ::=
"name:" _ StringLiteral
IndexType ::=
"type:" _ ( btree | hash )
btree ::=
"btree"
hash ::=
"hash"
inline_field_identifier ::=
( name "." name "." name )
| ( name "." name )
DefaultVal ::=
StringLiteral
| Expression
| BooleanLiteral
| NumberLiteral
BooleanLiteral ::=
'true'
| 'false'
| 'null'
NumberLiteral ::=
"-"? ( float | integer )
header_color ::=
"headercolor"
sharp ::=
"#"
hex_color ::=
six_char
| three_char
six_char ::=
hex_char hex_char hex_char hex_char hex_char hex_char
three_char ::=
hex_char hex_char hex_char
hex_char ::=
[0-9a-fA-F]
unique ::=
"unique"
as ::=
"as"
character ::=
[a-zA-Z0-9_]
quote ::=
'"'
type_name ::=
type_character+
| ( quote [^\"\n]+ quote )
type_character ::=
character
| [#x5B#x5D]
expression ::=
factor*
factor ::=
( character+ sp* "(" expression ")" )
| ( "(" expression ")" )
| ( exprCharNoCommaSpace+ ( sp* | "," | ");" | ( endline ");" ) ) )
| ( exprChar+ . )
exprCharNoCommaSpace ::=
[\'.a-zA-Z0-9_+-]
endline ::=
sp* newline
exprChar ::=
[\',.a-zA-Z0-9_+-`]
| sp
| newline
| tab
tab ::=
"\t"
allowed_chars ::=
_NOT_ ( '{' | '}' | whitespace_quote ) .
whitespace_quote ::=
[ \t\r\n\r\"]
whitespace ::=
[ \t\r\n\r]
single_line_comment ::=
"//" [^\n]*
multi_lime_comment ::=
"/*" ( _NOT_ "*/" . )* "*/"
DoubleStringCharacter ::=
( '\\' '"' )
| ( _NOT_ '"' SourceCharacter )
MultiLineStringCharacter ::=
"\\'"
| ( "\\" "\\"+ )
| ( "\\" "\n"? )
| ( _NOT_ "'''" SourceCharacter )
SingleStringCharacter ::=
"\\'"
| ( _NOT_ "'" SourceCharacter )
SourceCharacter ::=
.
digit ::=
[0-9]
decimal_point ::=
dot
dot ::=
'.'
equal ::=
'='
float ::=
[0-9]+ "." [0-9]+
integer ::=
[0-9]+
//Added tokens for railroad generation
_NOT_ ::= '!'
_AND_ ::= '&'
```
Contributor guide
Assessment
This issue has not been assessed yet.