Mercurial > repos > tabletprog
comparison modules/parser.tp @ 209:4b3b57f39f10
Implement zeroPlus macro
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 27 Nov 2013 23:36:24 -0800 |
parents | a1b4a2bc8d72 |
children | 32080f96c3a0 |
comparison
equal
deleted
inserted
replaced
208:a1b4a2bc8d72 | 209:4b3b57f39f10 |
---|---|
1 #{ | 1 #{ |
2 _applyMatch <- :fun tomatch { | |
3 fun: tomatch | |
4 } | |
2 expandClass <- :chars { | 5 expandClass <- :chars { |
3 if: (chars length) > 0 { | 6 if: (chars length) > 0 { |
4 pos <- 0 | 7 pos <- 0 |
5 inverted <- false | 8 inverted <- false |
6 if: (chars byte: 0) = ("^" byte: 0) { | 9 if: (chars byte: 0) = ("^" byte: 0) { |
88 } | 91 } |
89 } else: { | 92 } else: { |
90 print: "uh oh" | 93 print: "uh oh" |
91 } | 94 } |
92 } | 95 } |
93 alpha <- charClass: "a-zA-Z" | 96 |
97 zeroPlus <- macro: :matchexpr { | |
98 funexpr <- false | |
99 valid <- false | |
100 matchcall <- if: (matchexpr nodeType) = "lambda" { | |
101 valid <- true | |
102 quote: (_applyMatch: matchexpr tomatch) | |
103 } else: { | |
104 if: (matchexpr nodeType) = "symbol" { | |
105 valid <- true | |
106 quote: (matchexpr: tomatch) | |
107 } | |
108 } | |
109 if: valid { | |
110 quote: :tomatch { | |
111 cur <- 0 | |
112 n <- tomatch byte_length | |
113 orig <- tomatch | |
114 match <- true | |
115 while: { match && cur < n } do: { | |
116 res <- matchcall | |
117 match <- res matched? | |
118 if: match { | |
119 //TODO: Use some kind of lightweight substring wrapper here | |
120 tomatch <- tomatch from: (res matchlen) | |
121 cur <- cur + (res matchlen) | |
122 } | |
123 } | |
124 if: cur > 0 { | |
125 #{ | |
126 matched? <- { true } | |
127 matchlen <- { cur } | |
128 } | |
129 } else: { | |
130 #{ | |
131 matched? <- { false } | |
132 } | |
133 } | |
134 } | |
135 } else: { | |
136 print: "#error Invalid zeroPlus macro call\n" | |
137 } | |
138 } | |
139 | |
140 | |
141 _alpha <- charClass: "a-zA-Z" | |
142 alpha <- zeroPlus: _alpha | |
143 alphaNum <- zeroPlus: (charClass: "a-zA-Z0-9") | |
94 | 144 |
95 main <- { | 145 main <- { |
96 cmatch <- alpha: "c0123" | 146 cmatch <- alpha: "czx0123" |
97 zeromatch <- alpha: "01234" | 147 zeromatch <- alpha: "01234" |
98 if: (cmatch matched?) { | 148 if: (cmatch matched?) { |
99 print: "c0123 matched with length " . (cmatch matchlen) . "\n" | 149 print: "czx0123 matched with length " . (cmatch matchlen) . "\n" |
100 } else: { | 150 } else: { |
101 print: "c0123 didn't match\n" | 151 print: "czx0123 didn't match\n" |
102 } | 152 } |
103 if: (zeromatch matched?) { | 153 if: (zeromatch matched?) { |
104 print: "0123 matched with length " . (zeromatch matchlen) . "\n" | 154 print: "0123 matched with length " . (zeromatch matchlen) . "\n" |
105 } else: { | 155 } else: { |
106 print: "0123 didn't match\n" | 156 print: "0123 didn't match\n" |
107 } | 157 } |
158 zeromatchanum <- alphaNum: "01234" | |
159 if: (zeromatchanum matched?) { | |
160 print: "01234 matched with length " . (zeromatchanum matchlen) . "\n" | |
161 } else: { | |
162 print: "01234 didn't match\n" | |
163 } | |
108 } | 164 } |
109 } | 165 } |