Beispiel #1
0
				public static AST.INode Parse(CharStream stream)
				{
					var startingPos = stream.Position;
					
					try
					{
						var condition = Expressions.Logic.Parse(stream);

						// check if we have an IfElse case or simply a logical expression
						string s;
						if (stream.ReadReg(@"\s*\?\s*", out s))
						{
							var first = Expression.Parse(stream);
							WhiteSpace.Parse(stream, true);
							stream.SkipCharacter(':');
							WhiteSpace.Parse(stream, true);
							var second = Expression.Parse(stream);
							return new AST.Conditional(condition, first, second);
						} else
						{ // it's simply a logical expression
							return condition;
						}
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing an <expression> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Beispiel #2
0
				public static Info Parse(CharStream stream, Info expected = null)
				{
					string output;
					int pos = stream.Position;

					if (!stream.ReadReg("(\'|\")", out output))
					{
						throw new Exceptions.ParseException(
							String.Format(
							"expected to read a <quote> (starting at {0}), but no characters were left",
							stream.ComputeDetailedPosition(pos)));
					}

					Info info;
					if (output[0] == '"')
					{
						info = new Info(Type.Double);
					} else 
					{
						info = new Info(Type.Single);
					}

					if (expected != null && expected.CompareTo(info) != 0)
					{
						throw new Exceptions.ParseException(
							String.Format(
							"expected to read {0} (starting at {1}), but found {2}",
							expected.ToString(),
							stream.ComputeDetailedPosition(pos),
							info.ToString()));
					}

					return info;
				}
Beispiel #3
0
					public static AST.INode Parse(CharStream stream)
					{
						var startingPos = stream.Position;

						try
						{
							var first = Binary.Parse(stream);
							string op;
							if (stream.ReadReg(@"\s*(\|\||\&\&)", out op))
							{
								WhiteSpace.Parse(stream, true);
								var second = Logic.Parse(stream);
								return new AST.LogicExpression(
									first, second, op.Trim());
							} else
							{
								return first;
							}
						} catch (Exception e)
						{
							string msg = String.Format(
								"something went wrong parsing an <logical_expression> starting at {0}",
								stream.ComputeDetailedPosition(startingPos));
							throw new Exceptions.ParseException(msg, e);
						}
					}
Beispiel #4
0
				public static AST.INode Parse(CharStream stream)
				{
					string raw;
					if (!stream.ReadReg(@"[\-\+]?[0-9]+", out raw))
					{
						throw stream.CreateException("a number literal whas expected");
					}

					return new AST.Literal(raw);
				}
Beispiel #5
0
				public static bool PeekAndParse(CharStream stream, out string identifier, bool allow_underscore)
				{
					var reg = allow_underscore ? @"[_a-zA-Z]\w*" : @"[a-zA-Z]\w*";
					if (!stream.EndOfStream() && stream.ReadReg(reg, out identifier))
					{
						return true;
					}

					identifier = null;
					return false;
				}
Beispiel #6
0
						// The constructor collects all the nodes
						public Chain(CharStream stream, AST.INode node, string op)
						{
							m_Nodes = new List<Node>(2);

							do
							{
								m_Nodes.Add(new Node(node, op));
								WhiteSpace.Parse(stream, true);
								node = Unary.Parse(stream);
							} while(stream.ReadReg (@"\s*(\=\=|\!\=|\<\=|\>\=|\<|\>|\+|\-|\*|\/|\%)", out op));
						
							m_Nodes.Add(new Node(node));
						}
Beispiel #7
0
					public static AST.INode Parse(CharStream stream)
					{
						var startingPos = stream.Position;
						
						try
						{
							var first = Unary.Parse(stream);
							string raw;
							if (stream.ReadReg(@"\s*(\=\=|\!\=|\<\=|\>\=|\<|\>|\+|\-|\*|\/|\%)", out raw))
							{
								var chain = new Chain(stream, first, raw);
								return chain.Build();
							} else
							{
								return first;
							}
						} catch (Exception e)
						{
							string msg = String.Format(
								"something went wrong parsing an <binary_expression> starting at {0}",
								stream.ComputeDetailedPosition(startingPos));
							throw new Exceptions.ParseException(msg, e);
						}
					}
Beispiel #8
0
				private static char ReadUnicodeCharacter(CharStream stream)
				{
					string unicodeValue;
					if (stream.ReadReg("(0|1)?[0-9a-fA-F]{4,5}", out unicodeValue))
					{
						return (char)Convert.ToInt32(unicodeValue, 16);
					} else
					{
						throw stream.CreateException("not a valid unicode character");
					}
				}