Skip to content

Commit

Permalink
[LANGUAGE] improve error message for unquoted string with comma (#4786)
Browse files Browse the repository at this point in the history
Fixes #3967


**How to test**

Check the error from  #3967 is improved (I have also added tests)
  • Loading branch information
Felienne authored and rix0rrr committed Nov 25, 2023
1 parent 4743bc5 commit 3a1853c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4,522 deletions.
9 changes: 8 additions & 1 deletion grammars/level4-Additions.lark
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// redefining it entirely since it has many order-depending rules (e.g ask_no_quotes should be after ask and before assign)
command: clear | print | ask | turtle | assign_list | add | remove | sleep | error_list_access | error_ask_no_quotes| assign | error_invalid_space | error_print_no_quotes | error_text_no_print |error_invalid | empty_line
command: clear | print | ask | turtle | assign_list | add | remove | sleep | error_list_access | error_ask_no_quotes| assign | error_invalid_space | error_print_no_quotes | error_print_one_quote_only | error_text_no_print |error_invalid | empty_line

print: _PRINT (_print_argument)? -> print
error_print_no_quotes: _PRINT text -> error_print_nq

textwithoutsinglequote: /([^\n']+)/ -> text
textwithoutdoublequote: /([^\n"]+)/ -> text
error_print_one_quote_only: _PRINT "'" textwithoutsinglequote -> error_print_nq | _PRINT "\"" textwithoutdoublequote -> error_print_nq | _PRINT textwithoutsinglequote "'" -> error_print_nq | _PRINT textwithoutdoublequote "\"" -> error_print_nq


// FH, feb 2023 Shall we rename this 'var' to declare maybe? I confused myself :)
ask: var _IS _ASK (_print_argument)?
error_ask_no_quotes: var _IS _ASK text -> error_print_nq
Expand All @@ -20,6 +25,8 @@ error_text_no_print: quoted_text
textwithoutspaces: /([^\n،,,、 ]+)/ -> text




quoted_text: (/'((?:[^\\']|\\.)*)'/ | /"((?:[^\\"]|\\.)*)"/ | /((?:[^\\]|\\.)*)/ | /((?:[^\\]|\\.)*)/ | /«((?:[^\\»]|\\.)*)»/ ) -> text //text can be between single or double quotes, but quotes may be escaped with \


2 changes: 1 addition & 1 deletion grammars/level5-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ nospace: /[^\n, ]/
textwithspaces: /(?:[^#\n،,,、 ]| (?!else|başka|faese|अन्यथा|否则|muuten|muidu|senão|ellers|alie|altfel|иначе|inače|altrimenti|інакше|lainnya|arall|annars|değilse|anders|ndryshe|inaczej|sinon|ไม่อย่างนั้น|sonst|లేకపోతే|sino|ورنہ|وکھرا|אחרת|وإلا))+/ -> text //anything can be parsed except for a newline and a comma for list separators
//a space is allowed, but it may not be followed by an else. The part " (?!else))" means space not followed by (negative look ahead) else
//That is because allowing else in strings leads to issue #303
textwithoutspaces: /(?:[^#\n،,,、 *+\-\/eidіиలేไamfnsbअ否אو]|א(?!חרת )|و(?!إلا |کھرا |رنہ)|(?!)|(?!न्यथा )|и(?!наче )|m(?!uidu |uuten )|d(?!eğilse )|b(?!aşka )|n(?!dryshe )|(?!ม่อย่างนั้น )|l(?!ainnya )|f(?!aese )|e(?!lse |llers )|s(?!inon |enão |ino |onst )|і(?!накше )|i(?!naczej |nače )|లే (?!కపోతే )|a(?!nders |lie |ltfel |ltrimenti |nnars |rall ))+/ -> text //anything can be parsed except for spaces (plus: a newline and a comma for list separators)
textwithoutspaces: /(?:[^#\n،,,、 *+\-\/eidіиlలేไamfnsbअ否אو]|א(?!חרת )|و(?!إلا |کھرا |رنہ)|(?!)|(?!न्यथा )|и(?!наче )|m(?!uidu |uuten )|d(?!eğilse )|b(?!aşka )|n(?!dryshe )|(?!ม่อย่างนั้น )|l(?!ainnya )|f(?!aese )|e(?!lse |llers )|s(?!inon |enão |ino |onst )|і(?!накше )|i(?!naczej |nače )|లే (?!కపోతే )|a(?!nders |lie |ltfel |ltrimenti |nnars |rall ))+/ -> text //anything can be parsed except for spaces (plus: a newline and a comma for list separators)
//the part e(?!lse)|i(?!f)) means e not followed by lse, and i not followed by f
// this is because allowing else and if in invalid leads to ambiguity in the grammar
// note that the i's look similar but are not: inaczej versus інакше!
Expand Down
4 changes: 2 additions & 2 deletions grammars/level6-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ _print_argument: (_SPACE | quoted_text | list_access | expression | print_expres
?print_expression: var_access_print | error_unsupported_number | INT

// redefining it entirely since it has many order-depending rules
command: clear | print | turtle | add | remove | sleep | error_print_no_quotes | ifpressed_else | error_ifpressed_missing_else | ifelse | ifs | ask | error_ask_no_quotes| assign_button | assign | assign_list | error_invalid_space | error_text_no_print | empty_line
_if_less_command: print | turtle | add | remove | sleep | error_print_no_quotes | ask | error_ask_no_quotes | assign_button | assign | assign_list
command: clear | print | turtle | add | remove | sleep | error_print_no_quotes | error_print_one_quote_only | ifpressed_else | error_ifpressed_missing_else | ifelse | ifs | ask | error_ask_no_quotes| assign_button | assign | assign_list | error_invalid_space | error_text_no_print | empty_line
_if_less_command: print | turtle | add | remove | sleep | error_print_no_quotes | error_print_one_quote_only | ask | error_ask_no_quotes | assign_button | assign | assign_list

//splitting these commands into two rules, one for equals and one for is so they can be properly handled in the translator
ask: var (_IS | _EQUALS) _ASK (_print_argument)?
Expand Down
Loading

0 comments on commit 3a1853c

Please sign in to comment.