Exemplo n.º 1
0
		public Token (Tokenizer t, string text, int offset, int line, int ch, TokenType type)
		{
			Owner = t;
			Text = text;
			Offset = offset;
			Line = line;
			Column = ch;
			Type = type;
		}
Exemplo n.º 2
0
		public Token (Tokenizer o, char c, int offset, int line, int ch)
		{
			Owner = o;
			Text = c.ToString ();
			Offset = offset;
			Line = line;
			Column = ch;
			Type = TokenType.None;

			if (o.PrimitiveDecider != null) {
				Type = o.PrimitiveDecider (c);
			}

			if (Type == TokenType.None) {
				if (char.IsLetterOrDigit (c) || c == '_')
					Type = TokenType.Alphanumeric;
				else {
					if (Owner.IsQuoteToken (c.ToString (), 0)) {
						Type = TokenType.StringQuoteStart;

					} else if (Owner.IsCommentToken (c.ToString (), 0)) {
						Type = TokenType.CommentStart;

					} else if (!Owner.IgnoreSpecial &&
						   (Owner.MaybeQuoteToken (c.ToString (), 0) ||
						   Owner.MaybeCommentToken (c.ToString (), 0))) {
						Type = TokenType.MaybeSpecial;
					}

					if (Type == TokenType.None)
						Type = TokenType.Symbolic;
				}

				foreach (char t in Owner.WhitespaceChars)
					if (c == t)
						Type = TokenType.Whitespace;
			}
		}
Exemplo n.º 3
0
		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;
		}
Exemplo n.º 4
0
		public void ReadSectionOpen (Tokenizer t)
		{
			Token tok = t.Read ();

			if (tok == null)
				throw new UnexpectedEndOfFileException (t, "{");

			if (tok.Text != "{")
				throw new UnexpectedTokenException (tok, "{");
		}
Exemplo n.º 5
0
		public string ReadEqString (Tokenizer t)
		{
			ReadEq (t);
			return ReadString (t);
		}
Exemplo n.º 6
0
		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);
			}
		}
Exemplo n.º 7
0
		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;
		}
Exemplo n.º 8
0
		public int ReadEqInt (Tokenizer t)
		{
			ReadEq (t);
			return ReadInt (t);
		}
Exemplo n.º 9
0
		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;
		}
Exemplo n.º 10
0
		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;
		}
Exemplo n.º 11
0
		public UnexpectedEndOfFileException (Tokenizer t, string expectedToken)
		{
			this.Tokenizer = t;
			this.ExpectedToken = expectedToken;
		}
Exemplo n.º 12
0
		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;
		}