Skip to content

Commit

Permalink
Grammar: add descriptions to productions for error messages
Browse files Browse the repository at this point in the history
The obvious strategy of adding a description to every production does
not work well, as when constructing an error message, Ohm seems to
stop at the first description. So for example, it would produce:

  Expected an expression

rather than something like

  Expected "+", "-", "(", a number, a boolean,…

In extreme cases it says:

  Expected a

which seems wrong!
  • Loading branch information
rrthomas committed Aug 27, 2024
1 parent 31fea5b commit a499a2e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
28 changes: 14 additions & 14 deletions src/grammar/ursa.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ Ursa {
Definition = ident Initializer
Initializer = "=" Exp

List = "[" ListOf<Exp, ","> ","? "]"
List (a list) = "[" ListOf<Exp, ","> ","? "]"

Object = NamedType? "{" ListOf<Definition, #sc> #sc "}"
Object (an object) = NamedType? "{" ListOf<Definition, #sc> #sc "}"

Map = "{" ListOf<KeyValue, ","> ","? "}"
Map (a map) = "{" ListOf<KeyValue, ","> ","? "}"
KeyValue = Exp ":" Exp

PostfixExp
Expand All @@ -45,8 +45,8 @@ Ursa {
Ifs = NonemptyListOf<If, else> (else Block)?
If = if Exp Block

Fn = FnType Block
FnType = (fn | gen) "(" ListOf<Param, ","> ","? ")" TypeAnnotation?
Fn (a function) = FnType Block
FnType (a function type) = (fn | gen) "(" ListOf<Param, ","> ","? ")" TypeAnnotation?
Param = ident TypeAnnotation?

Loop = loop Block
Expand Down Expand Up @@ -127,7 +127,7 @@ Ursa {

Use = use Path

Block = "{" Sequence "}"
Block (a block) = "{" Sequence "}"

// Types
NamedType = Path TypeArgs?
Expand Down Expand Up @@ -186,7 +186,7 @@ Ursa {
reservedWord = bool | null | keyword

// Make ident indirect so we can easily get a node with the whole identifier.
ident = identName
ident (an identifier) = identName
identName = ~reservedWord identifierStart identifierPart*
identifierStart = letter | "_"
identifierPart = identifierStart | digit
Expand All @@ -195,13 +195,13 @@ Ursa {

null = "null" ~identifierPart

bool = ("false" | "true") ~identifierPart
bool (a boolean) = ("false" | "true") ~identifierPart

number
number (a number)
= digit* "." digit+ -- fract
| digit+ -- whole

string = "\"" stringCharacter* "\""
string (a string) = "\"" stringCharacter* "\""
stringCharacter
= ~("\"" | "\\" | lineTerminator) any -- nonEscaped
| "\\" escapeSequence -- escaped
Expand All @@ -224,19 +224,19 @@ Ursa {
= "x" hexDigit hexDigit
unicodeEscapeSequence
= "u" hexDigit hexDigit hexDigit hexDigit
lineTerminator
lineTerminator (a line terminator)
= "\n" | "\r" | "\x0B" | "\x0C" | "\u2028" | "\u2029"
lineTerminatorSequence
= "\n" | "\r" ~"\n" | "\x0B" | "\x0C" | "\u2028" | "\u2029" | "\r\n"

literalString
literalString (a literal string)
= "r\"" (~"\"" any)* "\""
| "r#\"" (~"\"#" any)* "\"#"
| "r##\"" (~"\"##" any)* "\"##"
| "r###\"" (~"\"###" any)* "\"###"
| "r####\"" (~"\"####" any)* "\"####"

comment = blockComment | lineComment
comment (a comment) = blockComment | lineComment
blockComment = "/*" (~("*/" | "/*") any)* blockComment? (~"*/" any)* "*/"
lineComment = "//" (~lineTerminator any)*

Expand All @@ -246,7 +246,7 @@ Ursa {
// Does not accept lineTerminators
spacesNoNL = (whitespace | comment)*

whitespace
whitespace (whitespace)
= "\t"
| " "
| "\u00A0" -- noBreakSpace
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/ursa.ohm-bundle.d.ts.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- src/grammar/ursa.ohm-bundle.d.ts 2024-08-20 16:44:30.436461339 +0100
+++ src/grammar/ursa.ohm-bundle.d.part-patched.ts 2024-08-20 16:44:30.564461957 +0100
--- src/grammar/ursa.ohm-bundle.d.ts 2024-08-27 21:08:34.518891120 +0100
+++ src/grammar/ursa.ohm-bundle.d.part-patched.ts 2024-08-27 21:08:34.658891700 +0100
@@ -4,14 +4,33 @@
import {
BaseActionDict,
Expand Down
2 changes: 1 addition & 1 deletion test/repl-syntax-error.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Line 1, col 4:
> 1 | 4 +
^
Expected "(", "gen", "fn", "{", "[", "_", a letter, a digit, ".", "r####\"", "r###\"", "r##\"", "r#\"", "r\"", "\"", "true", "false", "null", "-", "+", or "~"
Expected "(", a function, a block, a list, an identifier, an object, a map, a number, a literal string, a string, a boolean, "null", "-", "+", or "~"

0 comments on commit a499a2e

Please sign in to comment.