This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.fs
77 lines (69 loc) · 1.96 KB
/
parser.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
// possum v2.0.0
// copyright (c) 2011-2012 darkf
// licensed under the terms of the MIT license
// see LICENSE for details
module Parser
open Possum
open Types
let getNode acc =
match acc with
| "true" -> BoolNode true
| "false" -> BoolNode false
| "nil" -> NilNode
| _ ->
try
IntegerNode (System.Int32.Parse acc)
with
_ ->
AtomNode acc
let parseString (str : string) =
let mutable acc = ""
let mutable inString = false
let mutable inAtom = false
let mutable inEscape = false
let mutable inComment = false
let mutable xs = []
for i in 0..str.Length-1 do
match str.[i] with
| '(' when str.[i+1] = '*' && not inComment ->
// begin comment
inComment <- true
| ')' when str.[i-1] = '*' && inComment ->
// end comment
inComment <- false
| _ when inComment -> ()
// escape codes
| '\\' when inString && not inEscape -> inEscape <- true
| 'n' | 'r' | 't' | '"' | '\\' when inEscape ->
acc <- acc + (match str.[i] with
| 'n' -> "\n"
| 'r' -> "\r"
| 't' -> "\t"
| '"' -> "\""
| '\\' -> "\\" | _ -> "")
inEscape <- false
// end string
| '"' when inString = true ->
xs <- xs @ [StringNode acc]
inString <- false
acc <- ""
// begin string
| '"' when not inString && not inAtom->
inString <- true
// whitespace
| ' ' | '\t' | '\r' | '\n' when not inString ->
if acc.Length > 0 then
xs <- xs @ [getNode acc]
acc <- ""
inAtom <- false
| c ->
acc <- acc + (string c)
if not inString then
inAtom <- true
if acc.Length > 0 then
xs <- xs @ [getNode acc]
xs
let parseFile (filename : string) =
use sr = new System.IO.StreamReader(filename)
let txt = sr.ReadToEnd ()
parseString txt