no-context / moo

Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo.
BSD 3-Clause "New" or "Revised" License
814 stars 65 forks source link

Display context and mark out error tokens #145

Open aliclark opened 3 years ago

aliclark commented 3 years ago

This PR adds a graphical representation of the last few lines before and error and highlighting of the error token from underneath, intended to make it easier to understand which part of the print out was cause of the error.

Some examples (using Moo with Nearley):

Before:

Error: Syntax error at line 4 col 1:

  ^

After:

Error: Syntax error at line 4 col 1:

     1  
     2  
     3  new of self with iterable:
     4  
        ^       

Before:

Error: Syntax error at line 6 col 5:

  note:
      ^

After:

Error: Syntax error at line 6 col 5:

     2  use software,
     3      category
     4  
     5  
     6  note:`This is the main software`
            ~                           

Before:

Error: Syntax error at line 35 col 11:

  square of x
            ^

After:

Error: Syntax error at line 35 col 11:

    31          otherwise:
    32              result 'stopped'
    33  
    34  
    35  square of x:
                  ~ 

Before:

Error: Syntax error at line 11 col 30:

    for each number in infinity, 
                               ^

After:

Error: Syntax error at line 11 col 30:

     7          collect self + 1 + number
     8  
     9  
    10  numbersLessThan self:
    11      for each number in infinity, with extent: self,
        \tab                            ~                  
aliclark commented 3 years ago

@tjvr @nathan any chance for this change to get merged?

I could create a scoped package otherwise but would prefer not to

tjvr commented 3 years ago

I think I would prefer to merge #129, sorry!