public void ReadSectionOpen (Tokenizer t) { Token tok = t.Read (); if (tok == null) throw new UnexpectedEndOfFileException (t, "{"); if (tok.Text != "{") throw new UnexpectedTokenException (tok, "{"); }
public Keymap ReadMap (Tokenizer t) { Keymap map = new Keymap (); Token tok; ReadSectionOpen (t); do { int scancode, key; tok = t.Read (); if (tok == null) throw new UnexpectedEndOfFileException (t, "}"); if (tok.Text == "}") break; else t.PutBack (tok); scancode = ReadInt (t); key = ReadEqChar (t); ReadEnd (t); map.Entries [scancode] = key; } while (true); return map; }
public int ReadEqChar (Tokenizer t) { Token tok; ReadEq (t); tok = t.Read (); if (tok.Type == TokenType.StringQuoteStart) { string text; t.PutBack (tok); text = ReadString (t); text = text.Replace ("\\b", "\b"); text = text.Replace ("\\t", "\t"); text = text.Replace ("\\n", "\n"); text = text.Replace ("\\'", "'"); text = text.Replace ("\\\"", "\""); text = text.Replace ("\\\\", "\\"); if (text.Length != 1) throw new Exception (tok.PositionString + ": Expected one character, not string '" + text + "'"); return text [0]; } else if (tok.Text == "@") { tok = t.Read (); if (tok.Type != TokenType.Alphanumeric) throw new UnexpectedTokenException (tok, "<special-key>"); return (int) Enum.Parse (typeof (SpecialKeys), tok.Text); } else { t.PutBack (tok); return ReadInt (t); } }
public string ReadString (Tokenizer t) { Token tok = t.Read (); string text; if (tok == null) throw new UnexpectedEndOfFileException (t, "<quote-start>"); if (tok.Type != TokenType.StringQuoteStart) throw new UnexpectedTokenException (tok, "<quote-start>"); tok = t.Read (); if (tok == null) throw new UnexpectedEndOfFileException (t, "<string>"); if (tok.Type != TokenType.String) throw new UnexpectedTokenException (tok, "<string>"); text = tok.Text; tok = t.Read (); if (tok == null) throw new UnexpectedEndOfFileException (t, "<quote-end>"); if (tok.Type != TokenType.StringQuoteEnd) throw new UnexpectedTokenException (tok, "<quote-end>"); return text; }
public int ReadInt (Tokenizer t) { bool neg = false; int num; Token tok = t.Read (); if (tok.Text == "-") { neg = true; tok = t.Read (); } if (tok.Text == "0x") { string tempNum = ""; do { tok = t.Read (); if (tok.Type == TokenType.Alphanumeric) tempNum += tok.Text; else break; } while (true); try { num = int.Parse (tempNum, NumberStyles.HexNumber); } catch (FormatException) { throw new UnexpectedTokenException (tok, "<hex>"); } } else if (tok.Type == TokenType.Alphanumeric) { try { num = int.Parse (tok.Text); } catch (FormatException) { throw new UnexpectedTokenException (tok, "<dec>"); } } else throw new UnexpectedTokenException (tok, "<number>"); if (neg) return -num; else return num; }
public bool ParseStatement (Tokenizer t) { bool err = false; Token token = t.Read (); if (token == null) return false; if (token.Type == TokenType.Alphanumeric) { switch (token.Text) { case "keymask": keymask = ReadEqInt (t); break; case "statebit": statebit = ReadEqInt (t); break; case "default": Console.WriteLine ("Parsing `default' keymap..."); defaultMap = ReadMap (t); break; case "shifted": Console.WriteLine ("Parsing `shifted' keymap..."); shiftedMap = ReadMap (t); break; default: err = true; break; } } else err = true; if (err) throw new UnexpectedTokenException (token, "<unknown>"); return true; }
public int Parse (string file) { string content; Tokenizer t; int errors = 0; int line, col; if (file == null) throw new ArgumentNullException ("file"); using (StreamReader sr = new StreamReader (file)) content = sr.ReadToEnd (); t = new Tokenizer (content, true); t.CommentTokenPairs = new string [] { "//", "\n", "/*", "*/", }; t.QuoteTokenPairs = new string [] { "'", "'" }; t.SpecialTokens = new string [] { "0x" }; while (true) { bool failed = false; try { if (!ParseStatement (t)) break; } catch (UnexpectedTokenException ute) { failed = true; Console.Error.WriteLine ( "Unexpected token '{0}' on line {1}, col {2} (wanted '{3}')", ute.Token.Text, ute.Token.Line, ute.Token.Column, ute.ExpectedToken); } catch (UnexpectedEndOfFileException ueof) { failed = true; ueof.Tokenizer.GetLineInfo (out line, out col); Console.Error.WriteLine ( "Unexpected end of file at line {0}, col {1}; expected `{2}'", line, col, ueof.ExpectedToken); return 2; } if (failed) { Token tk; ++errors; if (errors >= MaxErrors) break; while ((tk = t.Read ()) != null) { if (tk.Text == ";") break; } } } if (errors > 0) return 1; return 0; }