-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.fsl
109 lines (102 loc) · 3.86 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
//
// IDLC - Interface Definition Language Compiler
//
// Copyright (C) 2018-2019 Wael El Oraiby
//
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
module Lexer
open FSharp.Text.Lexing
open System
open Parser
open Ast
exception SyntaxError of string
let lexeme (lexbuf : LexBuffer<char>) = new System.String(lexbuf.Lexeme)
let newline (lexbuf:LexBuffer<_>) = lexbuf.EndPos <- lexbuf.EndPos.NextLine
let getPosition streamName (lexbuf: LexBuffer<char>) =
{ Position.streamName = streamName
line = lexbuf.StartPos.Line + 1
column = lexbuf.StartPos.Column
cursor = lexbuf.StartPos.AbsoluteOffset }
}
let int = ['0'-'9']+
let digit = ['0'-'9']
let frac = '.' digit*
let exp = ['e' 'E'] ['-' '+']? digit+
let float = '-'? digit* frac? exp?
let char = '\'' ( [^'\\'] | ('\\' ( '\\' | '\'' | "\"" | 'n' | 't' | 'b' | 'r'))) '\''
let charUpper = ['A'-'Z']
let charLower = ['a'-'z']
let charAll = ['A'-'Z'] | ['a' - 'z'] | ['0'-'9']
let typeName = charUpper charAll*
let identName = charLower charAll*
let white = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
rule read streamName =
parse
| white { read streamName lexbuf }
| newline { newline lexbuf; read streamName lexbuf }
| int { INT (int (lexeme lexbuf)) }
| float { FLOAT (float (lexeme lexbuf)) }
| "Unit" { UNIT }
| "Bool" { BOOL }
| "Char" { CHAR }
| "U8" { U8 }
| "U16" { U16 }
| "U32" { U32 }
| "U64" { U64 }
| "S8" { S8 }
| "S16" { S16 }
| "S32" { S32 }
| "S64" { S64 }
| "F32" { F32 }
| "F64" { F64 }
| typeName { let pos = getPosition streamName lexbuf in TYPENAME (lexeme lexbuf, pos) }
| "interface" { INTERFACE }
| "object" { OBJECT }
| "import" { IMPORT }
| "fn" { FN }
| "struct" { STRUCT }
| "enum" { ENUM }
| "union" { UNION }
| "{" { LBRACE }
| "}" { RBRACE }
| "[" { LSQB }
| "]" { RSQB }
| "(" { LPAREN }
| ")" { RPAREN }
| "->" { RARROW }
| "," { COMMA }
| ";" { SC }
| ":" { COL }
| "*" { MUL }
| "=" { EQ }
| "." { DOT }
| "-" { MINUS }
| eof { EOF }
| "//" { readComment streamName lexbuf }
| identName { let pos = getPosition streamName lexbuf in IDENTIFIER (lexeme lexbuf, pos) }
| _ { raise (Exception (sprintf "SyntaxError: Unexpected char: '%s' Line: %d Column: %d" (lexeme lexbuf) (lexbuf.StartPos.Line+1) lexbuf.StartPos.Column)) }
and readComment streamName =
parse
| newline { newline lexbuf; read streamName lexbuf }
| _ { readComment streamName lexbuf }