Help understanding some code in parser.ml

58 views
Skip to first unread message

Andy Somogyi

unread,
Aug 21, 2016, 4:10:13 PM8/21/16
to Haxe
Hi, 

I'm trying to understand how the haxe compiler works, and I've come across some interesting syntax and I'm trying to understand what it does. I'm reasonably well versed with F#/ocaml, but I've not encountered code like this. 

The parser.ml module uses the name 'parser' very frequently as a pattern, i.e. 

let semicolon s =

    if fst (last_token s) = BrClose then

        match s with parser

        | [< '(Semicolon,p) >] -> p

        | [< >] -> snd (last_token s)


Could anyone please explain what the statement "match s with parser" does? 


Or other uses of the term 'parser' such as


let comma = parser

    | [< '(Comma,_) >] -> ()

Simon Krajewski

unread,
Aug 21, 2016, 4:13:47 PM8/21/16
to haxe...@googlegroups.com
It's a camlp4o thing, it matches the semantics of matching against `function` except that it uses stream matching. See e.g. http://caml.inria.fr/pub/docs/tutorial-camlp4/tutorial002.html#toc6

Simon

Andy Somogyi

unread,
Aug 21, 2016, 4:19:54 PM8/21/16
to haxe...@googlegroups.com
Ah, that explains it

thanks!

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Andy Somogyi

unread,
Aug 21, 2016, 5:49:17 PM8/21/16
to haxe...@googlegroups.com
I’m working on porting the haxe parser to f#, so far I’ve got the Ast and lexer compiler, and have just started on the parser. 

Since the parser uses camlp4o which is completely new to me, this is going to be quite a bit of work, as the parser.ml is rather involved. 

I’ve found the haxeparser library written in haxe, which to me seems a lot easier to read than the ocaml version. 

Basically the initial goal is the read haxe into an AST and perform a number of operations on the ast in F#. Eventually, I want to make haxe a first class .net language and compile directly to MSIL, but I really would like to have something written in the shortest amount of time that just reads haxe and builds and AST. 

I’m wondering now, perhaps the haxeparser parser in haxe itself would be a better starting point than the parser.ml for porting to F#. 

What do you guys think. 


On Aug 21, 2016, at 4:13 PM, Simon Krajewski <si...@haxe.org> wrote:

Simon Krajewski

unread,
Aug 23, 2016, 10:17:07 AM8/23/16
to haxe...@googlegroups.com
Am 21.08.2016 um 23:49 schrieb Andy Somogyi:
> I’m working on porting the haxe parser to f#, so far I’ve got the Ast
> and lexer compiler, and have just started on the parser.
>
> Since the parser uses camlp4o which is completely new to me, this is
> going to be quite a bit of work, as the parser.ml is rather involved.
>
> I’ve found the haxeparser library written in haxe, which to me seems a
> lot easier to read than the ocaml version.
>
> Basically the initial goal is the read haxe into an AST and perform a
> number of operations on the ast in F#. Eventually, I want to make haxe
> a first class .net language and compile directly to MSIL, but I really
> would like to have something written in the shortest amount of time
> that just reads haxe and builds and AST.
>
> I’m wondering now, perhaps the haxeparser parser in haxe itself would
> be a better starting point than the parser.ml for porting to F#.
>
> What do you guys think.

Well, there's nothing magical going on in parser.ml, it's a pretty
standard recursive descent parser implementation. Haxeparser is
basically the same, just with a different syntax.

How do you typically write parsers in F#? Maybe we can collaborate on
this and finally get a proper EBNF-like notation for Haxe's syntax which
you can use.

Simon

Andy Somogyi

unread,
Aug 23, 2016, 12:18:43 PM8/23/16
to haxe...@googlegroups.com
F# is basically just ocaml, its probably about 95% compatible, except it doesn’t support any ocaml extensions.

Eventually I want to write a PEG parser, but currently, I just want to throw together something as quickly as possible for a demo, so what I’m writing now is all throw-away code.

I’m just following the parser.ml and looking at the haxe version where the caml4o bits are unclear.

I think most people just write recursive decent parsers, but there is fsyacc/fslex (which are ports of the ocaml yacc/lex), but I’m not a huge fan of yacc.


Agreed, I think eventually it would be nice to have a EBNF grammar, but its probably going to be a while before I can think about it.
Reply all
Reply to author
Forward
0 new messages