Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grammar specification #73

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/extensions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## RON extensions

TODO
118 changes: 118 additions & 0 deletions docs/grammar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# RON grammar

This file describes the structure of a RON file in [EBNF notation][ebnf].
If extensions are enabled, some rules will be replaced. For that, see the
[extensions document][exts] which describes all extensions and what they override.

[ebnf]: https://en.wikipedia.org/wiki/Extended_Backus–Naur_form
[exts]: ./extensions.md

## RON file

```ebnf
RON = [extensions], ws, value, ws
```

## Whitespace and comments

```ebnf
ws = { ws_single, comment }
ws_single = "\n" | "\t" | "\r" | " "
comment = ["//", { no_newline }, "\n"]
```

## Commas

```ebnf
comma = ws, ",", ws
```

## Extensions

```ebnf
extensions = { "#", ws, "!", ws, "[", ws, extensions_inner, ws, "]", ws }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any reason to support ws between # and !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what Rust allows, too. And that's what I implemented. Should I change the implementation?

extensions_inner = "enable", ws, "(", extension_name, { comma, extension_name }, [comma], ws, ")"
```

For the extension names see the [`extensions.md`][exts] document.

## Value

```ebnf
value = unsigned | signed | float | string | char | bool | option | list | map | tuple | struct | enum_variant
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about grammar specs, but can we have something that is more formal? E.g. something like ron.ebnf (or any other notation) that is parse-able and validate-able.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought about putting everything into one file and creating automated tests for our parser with it. But I don't have that much time and I think this is useful as is.

```

## Numbers

```ebnf
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
unsigned = digit, { digit }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got to support other numerical systems (I know we don't support them atm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

signed = ["+" | "-"], unsigned
float = float_std | float_frac
float_std = ["+" | "-"], digit, { digit }, ".", {digit}, [float_exp]
float_frac = ".", digit, {digit}, [float_exp]
float_exp = ("e" | "E"), digit, {digit}
```

## String

```ebnf
string = "\"", { no_double_quotation_marks | string_escape }, "\""
string_escape = "\\", ("\"" | "\\" | "b" | "f" | "n" | "r" | "t" | ("u", unicode_hex))
```

## Char

```ebnf
char = "'", (no_apostrophe | "\\\\" | "\\'"), "'"
```

## Boolean

```ebnf
bool = "true" | "false"
```

## Optional

```ebnf
option = "Some", ws, "(", ws, value, ws, ")"
```

## List

```ebnf
list = "[", [value, { comma, value }, [comma]], "]"
```

## Map

```ebnf
map = "{", [map_entry, { comma, map_entry }, [comma]], "}"
map_entry = value, ws, ":", ws, value
```

## Tuple

```ebnf
tuple = "(", [value, { comma, value }, [comma]], ")"
```

## Struct

```ebnf
struct = unit_struct | tuple_struct | named_struct
unit_struct = ident | "()"
tuple_struct = [ident], ws, tuple
named_struct = [ident], ws, "(", [named_field, { comma, named_field }, [comma]], ")"
named_field = ident, ws, ":", value
```

## Enum

```ebnf
enum_variant = enum_variant_unit | enum_variant_tuple | enum_variant_named
enum_variant_unit = ident
enum_variant_tuple = ident, ws, tuple
enum_variant_named = ident, ws, "(", [named_field, { comma, named_field }, [comma]], ")"
```