forked from cmatheja/fsharp-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.fsl
34 lines (32 loc) · 1.26 KB
/
Lexer.fsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// The generated lexer module will start with this code
{
open FSharp.Text.Lexing
open System
// open the module that defines the tokens
open Parser
// Set the language to English such that 4.0 is parsed as 4 and not 40.
System.Globalization.CultureInfo.CurrentCulture <- new System.Globalization.CultureInfo("en-US")
}
// We define macros for some regular expressions we will use later
let digit = ['0'-'9']
let num = digit+ ( '.' digit+)? ('E' ('+'|'-')? digit+ )?
let whitespace = [' ' '\t']
let newline = "\n\r" | '\n' | '\r'
// We define now the rules for recognising and building tokens
// for each of the tokens of our language we need a rule
// NOTE: rules are applied in order top-down.
// This is important when tokens overlap (not in this example)
rule tokenize = parse
// deal with tokens that need to be ignored (skip them)
| whitespace { tokenize lexbuf }
| newline { lexbuf.EndPos <- lexbuf.EndPos.NextLine; tokenize lexbuf; }
// deal with tokens that need to be built
| num { NUM(Double.Parse(LexBuffer<_>.LexemeString lexbuf)) }
| '*' { TIMES }
| '/' { DIV }
| '+' { PLUS }
| '-' { MINUS }
| '^' { POW }
| '(' { LPAR }
| ')' { RPAR }
| eof { EOF }