private void ParseBNFEntry(string s, ref int x) { BNFEntry ret = new BNFEntry(); ParseBNFWhitespace(s, ref x); BNFEntryReference name = ParseBNFToken(s, ref x); ret.Name = name.Value; if (ret.Name.EndsWith(":")) { ret.Name = ret.Name.TrimEnd(':'); } else { ParseBNFWhitespace(s, ref x); if (!BNFMatch(s, ref x, ":")) { throw new Exception(); } } ParseBNFWhitespace(s, ref x); bool cont = true; List <BNFEntryReference> entries = new List <BNFEntryReference>(); while (cont) { BNFEntryReference entry = ParseBNFToken(s, ref x); entries.Add(entry); ParseBNFWhitespace(s, ref x); if (BNFMatch(s, ref x, ";")) { ret.Children.Add(entries.ToArray()); cont = false; ParseBNFWhitespace(s, ref x); } else if (BNFMatch(s, ref x, "|")) { ret.Children.Add(entries.ToArray()); entries = new List <BNFEntryReference>(); ParseBNFWhitespace(s, ref x); } } bnf_entries.Add(ret.Name, ret); }
private void ParseBNFHeaders(string s, ref int x) { ParseBNFWhitespace(s, ref x); if (BNFMatch(s, ref x, "%token")) { ParseBNFWhitespace(s, ref x); while (!BNFMatch(s, ref x, "%%")) { BNFEntryReference tok_name = ParseBNFToken(s, ref x); tokens.Add(tok_name.Value); ParseBNFWhitespace(s, ref x); } } ParseBNFWhitespace(s, ref x); }
private int ParseToken(IList <Preprocessor.token> tokens, int x, BNFEntryReference[] bnr) { int orig_x = x; for (int i = 0; i < bnr.Length; i++) { if (x >= tokens.Count) { return(orig_x); } // Match the token BNFEntryReference tok = bnr[i]; switch (tok.Type) { case BNFEntryReference.type.Constant: if (tokens[x].value != tok.Value) { return(orig_x); } x++; break; case BNFEntryReference.type.TokenReference: x++; break; case BNFEntryReference.type.EntryReference: { int new_x = ParseToken(tokens, x, tok.Value); if (new_x == x) { return(orig_x); } x = new_x; } break; } } return(x); }
private BNFEntryReference ParseBNFToken(string s, ref int x) { BNFEntryReference ret = new BNFEntryReference(); StringBuilder sb = new StringBuilder(); if (s[x] == '\'') { ret.Type = BNFEntryReference.type.Constant; x++; while (s[x] != '\'') { sb.Append(s[x]); x++; } x++; ret.Value = sb.ToString(); return(ret); } else { while (!char.IsWhiteSpace(s[x])) { sb.Append(s[x]); x++; } ret.Value = sb.ToString(); if (tokens.Contains(ret.Value)) { ret.Type = BNFEntryReference.type.TokenReference; } else { ret.Type = BNFEntryReference.type.EntryReference; } return(ret); } }