Skip to content

Instantly share code, notes, and snippets.

@lynzrand
Last active January 16, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lynzrand/13fb644218f6c44b6492703a32343abb to your computer and use it in GitHub Desktop.
Save lynzrand/13fb644218f6c44b6492703a32343abb to your computer and use it in GitHub Desktop.
HOCON grammar
EOL -> '\n' | '\r' | '\r\n'
Whitespace -> ' ' | '\t'
EscapedSequence ->
| '\\' [bfnrt\\]
| '\u' [0-9a-fA-F]{4}
QuotedStringElement ->
| [^\\"]
| EscapedSequence
QuotedString ->
| '"' QuotedStringElement* '"'
| '""'
UnquotedString -> [^$"{}\[\]:=,+#`\^?!@#&\\]+
MultilineString -> '"""' .* '"""'
Numerical -> [0-9]+
Number ->
| Numerical
| Numerical '.' Numerical
| Numerical '.' [Ee] [+-]? Numerical
| Numerical '.' Numerical [Ee] [+-]? Numerical
DurationUnit ->
| 'ns' | 'nanosecond' | 'nanoseconds'
| 'us' | 'microsecond' | 'microseconds'
| 'ms' | 'millisecond' | 'milliseconds'
| 's' | 'second' | 'seconds'
| 'm' | 'minute' | 'minutes'
| 'h' | 'hour' | 'hours'
| 'd' | 'day' | 'days'
Duration -> Number DurationUnit
SizeUnit ->
| 'B' | 'b' | 'byte' | 'bytes'
| ‘kB' | 'kilobyte' | 'kilobytes'
| ‘MB' | 'megabyte' | 'megabytes'
| ‘GB' | 'gigabyte' | 'gigabytes'
| ‘TB' | 'terabyte' | 'terabytes'
| ‘PB' | 'petabyte' | 'petabytes'
| ‘EB' | 'exabyte' | 'exabytes'
| ‘ZB' | 'zettabyte' | 'zettabytes'
| ‘YB' | 'yottabyte' | 'yottabytes'
| ‘K' | 'k' | 'Ki' | 'KiB' | 'kibibyte' | 'kibibytes'
| ‘M' | 'm' | 'Mi' | 'MiB' | 'mebibyte' | 'mebibytes'
| ‘G' | 'g' | 'Gi' | 'GiB' | 'gibibyte' | 'gibibytes'
| ‘T' | 't' | 'Ti' | 'TiB' | 'tebibyte' | 'tebibytes'
| ‘P' | 'p' | 'Pi' | 'PiB' | 'pebibyte' | 'pebibytes'
| ‘E' | 'e' | 'Ei' | 'EiB' | 'exbibyte' | 'exbibytes'
| ‘Z' | 'z' | 'Zi' | 'ZiB' | 'zebibyte' | 'zebibytes'
| ‘Y' | 'y' | 'Yi' | 'YiB' | 'yobibyte' | 'yobibytes'
Size -> Number SizeUnit
DeterminedSubstitution -> '${' Key '}'
NonDeterminedSubstitution -> '${?' Key '}'
Substitution ->
| DeterminedSubstitution
| NonDeterminedSubstitution
ValueSegment ->
| Number
| Duration
| Size
| List
| Map
| Substitution
| UnquotedString
| QuotedString
| MultilineString
Value -> ValueSegment ( Whitespace? ValueSegment )*
Separator -> ':' | '='
KeyValuePair ->
| Key Whitespace? Separator Whitespace? Value
| Key Map
ElementSeparator ->
| Whitespace? EOL Whitespace?
| Whitespace? ',' Whitespace?
| Whitespace? ',' Whitespace? EOL Whitespace?
KeyValuePairs ->
| KeyValuePair
| KeyValuePair ElementSeparator
| KeyValuePair ElementSeparator KeyValuePairs
Map -> '{' WhiteSpace? KeyValuePairs WhiteSpace? '}'
Values ->
| Value
| Value ElementSeparator
| Value ElementSeparator Values
Array -> '[' Whitespace? Values Whitespace? ']'
Document ->
| Whitespace? EOF
| Whitespace? KeyValuePairs Whitespace? EOF
| Whitespace? Map Whitespace? EOF
| Whitespace? Array Whitespace? EOF
@asm0dey
Copy link

asm0dey commented Jan 16, 2020

Is this grammar full? Can't find includes here.

@lynzrand
Copy link
Author

lynzrand commented Jan 16, 2020 via email

@asm0dey
Copy link

asm0dey commented Jan 16, 2020

Is there any description of supported/unsopported features?

@asm0dey
Copy link

asm0dey commented Jan 16, 2020

Excuse me, one more question: what are your definitions of List and Key?

@lynzrand
Copy link
Author

Oh sorry, I thought this was in another repository (that one where I wrote a C0 compiler).

For HOCON, I no longer maintain this grammar definition (and the associating parser) as they are just one of my homeworks. All supported features are the ones listed.

Key is defined as:
Key -> UnquotedString | QuotedString

List is the same as Array, I was inconsistent while writing this.

@asm0dey
Copy link

asm0dey commented Jan 16, 2020

Well then you possibly will be interested to know that DeterminedSubstitution should use domething like '${' Key ('.' Key)? '}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment