Compile compiles string rules into their runnable elements. Returns true if everything is ok, false if there were compile errors.
(ps *State)
| 239 | // Compile compiles string rules into their runnable elements. |
| 240 | // Returns true if everything is ok, false if there were compile errors. |
| 241 | func (pr *Rule) Compile(ps *State) bool { |
| 242 | if pr.Off { |
| 243 | pr.SetProperty("inactive", true) |
| 244 | } else { |
| 245 | pr.DeleteProperty("inactive") |
| 246 | } |
| 247 | if pr.Rule == "" { // parent |
| 248 | pr.Rules = nil |
| 249 | pr.setsScope = false |
| 250 | return true |
| 251 | } |
| 252 | valid := true |
| 253 | rstr := pr.Rule |
| 254 | if pr.Rule[0] == '-' { |
| 255 | rstr = rstr[1:] |
| 256 | pr.reverse = true |
| 257 | } else { |
| 258 | pr.reverse = false |
| 259 | } |
| 260 | rs := strings.Split(rstr, " ") |
| 261 | nr := len(rs) |
| 262 | |
| 263 | pr.Rules = make(RuleList, nr) |
| 264 | pr.ExclFwd = nil |
| 265 | pr.ExclRev = nil |
| 266 | pr.noTokens = false |
| 267 | pr.onlyTokens = true // default is this.. |
| 268 | pr.setsScope = false |
| 269 | pr.tokenMatchGroup = false |
| 270 | pr.Order = nil |
| 271 | nmatch := 0 |
| 272 | ntok := 0 |
| 273 | curStInc := 0 |
| 274 | eoses := 0 |
| 275 | for ri := range rs { |
| 276 | rn := strings.TrimSpace(rs[ri]) |
| 277 | if len(rn) == 0 { |
| 278 | ps.Error(lexer.PosZero, "Compile: Rules has empty string -- make sure there is only one space between rule elements", pr) |
| 279 | valid = false |
| 280 | break |
| 281 | } |
| 282 | if rn == "!" { // exclusionary rule |
| 283 | nr = ri |
| 284 | pr.Rules = pr.Rules[:ri] |
| 285 | pr.CompileExcl(ps, rs, ri+1) |
| 286 | break |
| 287 | } |
| 288 | if rn[0] == ':' { |
| 289 | pr.tokenMatchGroup = true |
| 290 | } |
| 291 | rr := &pr.Rules[ri] |
| 292 | tokst := strings.Index(rn, "'") |
| 293 | if tokst >= 0 { |
| 294 | if rn[0] == '?' { |
| 295 | rr.Opt = true |
| 296 | } else { |
| 297 | rr.StInc = curStInc |
| 298 | rr.Match = true // all tokens match by default |
no test coverage detected