Arc Forumnew | comments | leaders | submitlogin
IDEA: Syntax checker
8 points by stefano 6117 days ago | 8 comments
When using a macro it sometimes happen to make an error and use a syntax not accepted by the macro. If the author of the macro didn't insert error detection with helpful messages associated, you'll waste a lot of time trying to find what's wrong. The problem is that adding such cheks is usually very boring. The idea then is to add to Arc a way to easily construct a syntax checker with meaningful error messages, in order to make such task immediate.


4 points by raymyers 6117 days ago | link

Macros that throw syntax errors at macro-expansion -- not a bad idea. I had actually experimented a bit with this.

  arc> (load "lib/treeparse.arc")
  nil
  arc> (let (s clause forms) nil
         (= forms (filt [list:cons 'do _] (many anything)))
         (= clause (filt car (list (seq anything forms))))
         (= s (filt [cons 'if _] (many clause)))
         (mac cond clauses (parse-all s clauses)))
  #3(tagged mac #<procedure>)

  arc> (cond nil 1 t 2)
  Parse error: extra tokens '(nil 1 t 2)
  nil

  arc> (cond (nil 1) (t 2))
  2
Not the most meaningful error message in the word, but it's a start.

-----

1 point by stefano 6116 days ago | link

Interesting. This could eventually evolve.

-----

1 point by raymyers 6115 days ago | link

As I have remarked before, treeparse was influenced by Haskell's parsec. One of the features of parsec is the optional infusion of more meaningful error messages into grammars. One could easily imagine adding this functionality to treeparse.

-----

1 point by almkglor 6115 days ago | link

Interesting. I suppose this means that (at least for the cps version of treeparse) a fail must also "return" an error value (potentially just a "Expected lit %c, not found"). Of course 'alt parsers would ignore the error message, and generate its own when all alternatives fail ("No alternatives found"). Hmm. I think if the fail continuation returned an error message, we could create a filt-style parser (for the cps case):

  (def onerr (parser msg)
    (fn (remaining pass fail)
      (parser remaining pass
        (fn (_) ; ignored message
          (fail msg)))))
For the monadic version, our old nil-as-fail code would have to be changed I suspect; basically instead of the old type Return = Return parsed remaining actions | nil, we'll need type Return = Return parsed remainig actions | Failed message .

-----

2 points by almkglor 6117 days ago | link

The main problem with having macros post errors is finding out the line number of the error.

Consider a macro which spans several lines of code. Which line had the error?

-----

1 point by stefano 6116 days ago | link

To discover the line of the error the interpreter should provide a way to associate every form or symbol passed to the macro with its line number. As an example there could be a function such as (line-num form-with-an-error) to get the line number.

-----

1 point by almkglor 6116 days ago | link

My "attachments" idea was conceived with this potential use too - jus attach the line number to the object, which acts just like the original object (i.e. matches with 'is, etc.).

-----

1 point by cchooper 6117 days ago | link

With all the parsing and pattern matching libs going around, I'm sure one of them could be converted into a syntax checker.

-----