-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserUtil.fs
80 lines (67 loc) · 2.42 KB
/
ParserUtil.fs
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
module ParserUtil
open System.IO
open System.Text
open Microsoft.FSharp.Text.Lexing
open AST
open Lexer
open Parser
// Parse a string. (A statement is parsed)
let parseString (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.Main Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d, %s\n" pos.Line pos.Column e.Message
failwith "parser termination"
// Parse a file. (A statement is parsed)
let parseFromFile filename =
if File.Exists(filename)
then parseString(File.ReadAllText(filename))
else invalidArg "ParserUtil" "File not found"
// Parse a string. (A declaration list is parsed)
let parseDecList (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.DecList Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d\n" pos.Line pos.Column
failwith "parser termination"
// Parse a file. (A declaration list is parsed)
let parseDecListFromFile filename =
if File.Exists(filename)
then parseDecList(File.ReadAllText(filename))
else invalidArg "ParserUtil" "File not found"
let parseExp (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.Exp Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d\n" pos.Line pos.Column
failwith "parser termination"
let parseDec (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.Dec Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d\n" pos.Line pos.Column
failwith "parser termination"
let parseStm (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.Stm Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d\n" pos.Line pos.Column
failwith "parser termination"
let parseStmList (text:string) =
let lexbuf = LexBuffer<_>.FromBytes(Encoding.UTF8.GetBytes(text))
try
Parser.StmList Lexer.tokenize lexbuf
with e ->
let pos = lexbuf.EndPos
printfn "Error near line %d, character %d\n" pos.Line pos.Column
failwith "parser termination"