2

Currently i am working on developing a parser in JavaScript that parses Haskell Language.I am studying Esprima.JS and will like to build on top of it that will parse Haskell Language.As per my understanding i need to start with a basic parts such numbers(how they are used in Haskell language) Operators and then advance options.I am very new to it any help with resources will work for me

I am following this pattern from Esprima.js

Token={
       //Haskell
        PosToken:10
//Should i have to declare all tokens of Haskell over here
       };
TokenName={};
TokenName[Toke.PosToken]='PosToken';
//Should i have to all tokens of Haskell Overhere
FnExprTokens=[//Assignment and other operators];

Syntax={
//Haskell Expressions
HsExp:'HsExp',
HsStmt:'HsStmt',
HsFieldUpdate: 'HsFieldUpdate',
//and so on for other expression types
};
PropertyKind={};
//Error Messages
Messages={
};

Regex={};

Should i follow this process or improve on other things.Will the flow be the same if i want to build upon Esprima.js(http://esprima.org/). Esprima.js is easy to follow along then acorn.js(Although less lines of code and little bit faster).I want to parse Haskell to Mozilla AST format.Any help will be appreciated.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Sant14
  • 302
  • 1
  • 13
  • 2
    The Haskell Platform comes with a library for parsing Haskell98 (maybe it is up to date with H2010?). I'd recommend using this for reference. Note most Haskell code in the wild uses GHC extensions - there is a similar library for parsing this code (haskell-src-exts) but it is much bigger. Also the parsers in both are LR parsers generated with a parser generator (Happy), if the Esprima.js parser is written by hand (top-down LL parser) you will need to do quite a bit of work to write a similar top-down parser. – stephen tetley Mar 09 '14 at 09:19

1 Answers1

1

How about instead of implementing your own Haskell parser, you reuse an existing one: there's a number of Haskell parsers written in Haskell:

Then, you can compile that into JavaScript using GHCJS which is a variant of GHC that compiles almost all of Haskell to JavaScript.

You can then simply call into the GHCJS generated JavaScript code from your "regular" JavaScript code.

The resulting JS code generated by GHCJS is not intended to be human readable, and it's also significantly larger than hand written JS (but Google Closure Compiler alleviates that a little bit), but performs comparably well.

As to the Mozilla AST format, you can either transform the resulting data structure to be palatable by JS from within your Haskell code, and then transform that to Mozilla AST, or transform to Mozilla AST directly in the Haskell code. Of course you might end up writing a lot of Haskell code in the end, which might or might not be what you desire, but given you're trying to parse Haskell in the first place, I'm assuming you're not enemies with it.

Community
  • 1
  • 1
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111