-
Notifications
You must be signed in to change notification settings - Fork 3
/
Parse.elm
162 lines (140 loc) · 5.07 KB
/
Parse.elm
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module Parse where
import SpreadsheetTypes exposing (..)
import Regex exposing (..)
import String exposing (toFloat,toInt,toUpper)
type NotMatched = NotMatched
type alias Parser err succ = String -> Result err succ
parse: Parser String CellContent
parse s =
case find (AtMost 1) (regex "^=(.*)") s |> List.map .submatches of
match::[]
-> parseFormula s
_
-> case parseExpression s of
Err _ -> Ok (Value (Text s))
Ok atom -> Ok (Value atom)
parseFormula s = (Ok (Value (Number 1337)))
{-
I am sure you can do this through a port
Parse and eval arithmetic expressions as JavaScript
Be as awesome as the angular guy
-}
parseExpression: Parser NotMatched Atom
parseExpression s = Err (NotMatched)
--todo: range union and intersect, cell range according to http://www.nsl.com/k/excel.k
type Token = Unknown String | FunctionName String | Bra | Ket | Comma
| PlusToken | MinusToken | MultToken | DivToken |PowToken
| NumberToken Float | TextToken String
| ReferenceToken Ref | Colon
| Quote
| Eq | Le | Gr| Neq| Leq| Greq
type alias Ref = (String, Int)
type alias Tokenizer = String -> (String, List Token)
stackifyTokens: List Token -> List (Token, Int)
stackifyTokens ts =
let stackifyTokensInternal ts n = case ts of
[]
-> []
x::xs
-> let d = case x of
Bra -> 1
Ket -> -1
_ -> 0
in (x,n+d) :: (stackifyTokensInternal xs (n+d))
in stackifyTokensInternal ts 0
{-
type Atom = Number Float | Text String | Error
type AST a op = Node op (List (AST a op)) | Leaf a
type CellContent = Value Atom | Reference GridId | Formula (AST CellContent Op)
-}
type TokenTree = AST (List Token) ()
tokens: String -> List Token
tokens s =
let
tokensInternal s acc = case s of
""
-> acc
_
->
let
tokenizers : List Tokenizer
tokenizers =
[tok "(" Bra
,tok ")" Ket
,tok "," Comma
,tok "+" PlusToken
,tok "-" MinusToken
,tok "*" MultToken
,tok "/" DivToken
,tok "^" PowToken
,floatTok
,ctok "(\\w+[\\w\\d\\.]*)\\(" (\s -> FunctionName (toUpper s) :: Bra::[])
,referenceTok
,tok ":" Colon
,tok "\"" Quote --"=";"<";">";"<>";"<=";">="
,tok "=" Eq
,tok "<" Le
,tok ">" Gr
,tok "<>" Neq
,tok "<=" Leq
,tok ">=" Greq
]
firstThatWorks remainingTokenizers s = case remainingTokenizers of
[]
-> ("", [Unknown s]) --can't consume!
t::ts
-> case t s of
(rem,[]) -> firstThatWorks ts s
(rem, tokens) -> (rem,tokens)
in case firstThatWorks tokenizers s of
(rem, tokens)
-> tokensInternal rem (acc++tokens)
in tokensInternal s []
floatTok = ctok "(\\d+(?:\\.\\d+)?)" (\n-> (case String.toFloat n of
Err e
-> TextToken n
Ok f
-> NumberToken f
)::[])
tok literalString out s =
simpleTok (ourRegex (Regex.escape literalString)) (out::[]) s
rtok regexString out s =
simpleTok (ourRegex regexString) (out::[]) s
ctok regexWithCaptureGroup out s =
captureTok (ourRegex regexWithCaptureGroup) (\s-> (out s)) s
ourRegex regexString = Regex.regex ("^(?: *)"++regexString++"(.*)")
-- regex of the form "^regex(.*)"
simpleTok regex outTokens s =
case find (AtMost 1) regex s of
match::[]
-> case match.submatches of
Just theUnparsedBit :: []
-> (theUnparsedBit, outTokens)
_
-> (s,[])
_
-> (s, [])
-- regex of the form "^regexWithOneCaptureGroup(.*)"
captureTok regex makeTokFromCaptureGroup s =
case find (AtMost 1) regex s of
match::[]
-> case match.submatches of
Just captureGroup :: Just theUnparsedBit :: []
-> (theUnparsedBit, makeTokFromCaptureGroup captureGroup)
_
-> (s,[])
_
-> (s, [])
referenceTok s = case find (AtMost 1) (ourRegex ("([a-zA-Z]+)(\\d+)")) s of
match::[]
-> case match.submatches of
Just rows::Just cols :: Just theUnparsedBit :: []
-> case String.toInt cols of
Err e
-> (s,[])
Ok f
-> (theUnparsedBit, [ReferenceToken (toUpper rows, f)])
_
-> (s,[])
_
-> (s, [])