Ejemplo n.º 1
0
				public static void Parse(
					CharStream stream, string identifier,
					Internal.LocaleContext.Builder builder)
				{
					var startingPos = stream.Position;

					try
					{
						// private identifiers start with an underscore
						// and can only be referenced from within an l20n file
						bool isPrivate = (identifier.IndexOf('_') == 0);

						// an optional index is possible
						AST.INode index = null;
						Index.PeekAndParse(stream, out index);

						// White Space is required
						WhiteSpace.Parse(stream, false);

						var valuePos = stream.Position;

						// Now we need the actual value
						var value = Value.Parse(stream);

						if ((value as IO.AST.HashValue) == null && index != null)
						{
							string msg = String.Format(
								"an index was given, but a stringValue was given, while a hashValue was expected",
								stream.ComputeDetailedPosition(valuePos));
							throw new Exceptions.ParseException(msg);
						}

						// an optional attributes collection is possible
						AST.Attributes attributes;
						Attributes.PeekAndParse(stream, out attributes);

						// White Space is optional
						WhiteSpace.Parse(stream, true);

						stream.SkipCharacter('>');
						
						var entityAST = new AST.Entity(identifier, isPrivate, index, value, attributes);
						try
						{
							var entity = (Objects.Entity)entityAST.Eval();
							builder.AddEntity(identifier, entity);
						} catch (Exception e)
						{
							throw new Exceptions.EvaluateException(
								String.Format("couldn't evaluate `{0}`", entityAST.Display()),
								e);
						}
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing an <entity> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 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;
				}
Ejemplo n.º 3
0
				public static AST.INode Parse(CharStream stream)
				{
					var startingPos = stream.Position;
					AST.INode expression = null;

					try
					{
						// skip opening tags
						stream.SkipString("{{");
						WhiteSpace.Parse(stream, true);

						// parse actual expression
						expression = Expression.Parse(stream);

						// skip closing tags
						WhiteSpace.Parse(stream, true);
						stream.SkipString("}}");

						return expression;
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing an <expander> starting at {0}, expression was: {1} ({2})",
							stream.ComputeDetailedPosition(startingPos),
							expression != null ? expression.Display() : "<null>",
							expression != null ? expression.GetType().ToString() : "null");
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 4
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);
					}
				}
Ejemplo n.º 5
0
				public static string Parse(CharStream stream)
				{
					var startingPos = stream.Position;
					
					try
					{
						var output = "";
						var quote = Quote.Parse(stream);

						char c;
						
						while ((c = stream.PeekNext()) != '\0')
						{
							if (c == '\\')
							{
								output += stream.ForceReadNext();
							} else if (Quote.Peek(stream, quote))
							{
								break; // un-escaped quote means we're ending the string
							}

							output += stream.ForceReadNext();
						}
						
						Quote.Parse(stream, quote);
						
						return output;
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing an <string_value> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 6
0
				public static void Parse(
					CharStream stream,
					string identifier, Internal.LocaleContext.Builder builder)
				{
					var startingPos = stream.Position;
					
					try
					{
						var macroAST = new AST.Macro(identifier);
						
						stream.SkipCharacter('(');
						WhiteSpace.Parse(stream, true);

						// variables are optional,
						// but we do have them, we need at least one (duh)
						if (Expressions.Variable.Peek(stream))
						{
							macroAST.AddParameter(Macro.ParseVariable(stream));

							// more than 1 is possible as well
							while (stream.SkipIfPossible(','))
							{
								WhiteSpace.Parse(stream, true);
								macroAST.AddParameter(Macro.ParseVariable(stream));
							}
						}

						stream.SkipCharacter(')');
						WhiteSpace.Parse(stream, false);

						stream.SkipCharacter('{');
						WhiteSpace.Parse(stream, true);

						// Parse the Actual Macro Expression
						macroAST.SetExpression(Expression.Parse(stream));

						WhiteSpace.Parse(stream, true);
						stream.SkipCharacter('}');
						WhiteSpace.Parse(stream, true);
						stream.SkipCharacter('>');

						// return the fully parsed macro
						try
						{
							var macro = (Objects.Macro)macroAST.Eval();
							builder.AddMacro(identifier, macro);
						} catch (Exception e)
						{
							throw new Exceptions.EvaluateException(
								String.Format("couldn't evaluate `{0}`", macroAST.Display()),
								e);
						}
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing a <macro> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 7
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);
						}
					}
Ejemplo n.º 8
0
				public static AST.HashValue.Item Parse(CharStream stream)
				{
					var startingPos = stream.Position;
					
					try
					{
						// check if a hash item is supposed to be a default
						bool isDefault = stream.SkipIfPossible('*');
						
						// parse the raw identifier (key)
						var identifier = Identifier.Parse(stream, false);
						
						// whitespace is optional
						WhiteSpace.Parse(stream, true);
						// the seperator char is required as it seperates the key and the value
						stream.SkipCharacter(':');
						// more optional whitespace
						WhiteSpace.Parse(stream, true);
						
						// get the actual value, which is identified by the key
						var value = Value.Parse(stream);
						
						return new AST.HashValue.Item(identifier, value, isDefault);
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing a <hash_item> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 9
0
				public static AST.INode Parse(CharStream stream)
				{
					var startingPos = stream.Position;
					
					try
					{
						// skip open char
						stream.SkipCharacter('[');

						// we need at least one index
						var index = new AST.Index(ParseExpression(stream));

						// others are optional
						while (stream.SkipIfPossible(','))
						{
							index.AddIndex(ParseExpression(stream));
						}

						// skip close char
						stream.SkipCharacter(']');

						return index;
					} catch (Exception e)
					{
						string msg = String.Format(
							"something went wrong parsing an <index> starting at {0}",
							stream.ComputeDetailedPosition(startingPos));
						throw new Exceptions.ParseException(msg, e);
					}
				}
Ejemplo n.º 10
0
					private static AST.INode ParseExpression(CharStream stream)
					{
						WhiteSpace.Parse(stream, true);
						var expression = Expression.Parse(stream);
						WhiteSpace.Parse(stream, true);
						return expression;
					}
 public StandardTokenizer(CharStream stream)
 {
     token_source = new StandardTokenizerTokenManager(stream);
     token = new Token();
     jj_ntk = - 1;
     jj_gen = 0;
     for (int i = 0; i < 1; i++)
         jj_la1[i] = - 1;
 }
Ejemplo n.º 12
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);
				}
Ejemplo n.º 13
0
				public static bool PeekAndParse(CharStream stream)
				{
					if (stream.PeekNext() != '/' || stream.PeekNext(1) != '*')
					{
						return false;
					}

					Comment.Parse(stream);
					return true;
				}
Ejemplo n.º 14
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;
				}
Ejemplo n.º 15
0
				public static bool PeekAndParse(CharStream stream, out AST.Attributes value)
				{
					if (KeyValuePair.Peek(stream))
					{
						value = Attributes.Parse(stream);
						return true;
					}

					value = null;
					return false;
				}
Ejemplo n.º 16
0
				public static string Parse(CharStream stream, bool allow_underscore)
				{
					string identifier;
					if (!Identifier.PeekAndParse(stream, out identifier, allow_underscore))
					{
						throw stream.CreateException(
							"expected to read an <identifier>, but non-word character was found");
					}

					return identifier;
				}
Ejemplo n.º 17
0
				public static bool PeekAndParse(CharStream stream, out AST.HashValue.Item item)
				{
					if (stream.PeekNext() == '*' || Identifier.Peek(stream))
					{
						item = HashItem.Parse(stream);
						return true;
					}

					item = null;
					return false;
				}
Ejemplo n.º 18
0
				public static bool PeekAndParse(CharStream stream, out string value)
				{
					if (StringValue.Peek(stream))
					{
						value = PureStringValue.Parse(stream);
						return true;
					}
					
					value = null;
					return false;
				}
Ejemplo n.º 19
0
				public static bool PeekAndParse(CharStream stream, out AST.Attributes.Item item)
				{
					if (KeyValuePair.Peek(stream))
					{
						item = KeyValuePair.Parse(stream);
						return true;
					}
					
					item = null;
					return false;
				}
Ejemplo n.º 20
0
					public static bool PeekAndParse(CharStream stream, out AST.INode expression)
					{
						if (stream.PeekNext() == '(' || Primary.Peek(stream))
						{
							expression = Parenthesis.Parse(stream);
							return true;
						}

						expression = null;
						return false;
					}
Ejemplo n.º 21
0
				public static bool PeekAndParse(
					CharStream stream, out AST.INode value)
				{
					if (StringValue.PeekAndParse(stream, out value))
						return true;
					
					if (HashValue.PeekAndParse(stream, out value))
						return true;

					return false;
				}
Ejemplo n.º 22
0
				public static bool PeekAndParse(CharStream stream, out AST.INode index)
				{
					if (stream.PeekNext() != '[')
					{
						index = null;
						return false;
					}
					
					index = Index.Parse(stream);
					return true;
				}
Ejemplo n.º 23
0
				private static AST.INode ParseExpression(CharStream stream)
				{
					// optional white space
					WhiteSpace.Parse(stream, true);
					// first expression
					var index = Expression.Parse(stream);
					// optional white space
					WhiteSpace.Parse(stream, true);

					return index;
				}
Ejemplo n.º 24
0
					public static bool PeekAndParse(
						CharStream stream, out AST.INode expression)
					{
						if (stream.PeekReg(@"(\$|\@|\_|)?\w+(\.|\[)"))
						{
							expression = Property.Parse(stream);
							return true;
						}
						
						expression = null;
						return false;
					}
Ejemplo n.º 25
0
				public static bool PeekAndParse(
					CharStream stream, out AST.INode value)
				{
					if (HashValue.Peek(stream))
					{
						value = HashValue.Parse(stream);
						return true;
					}

					value = null;
					return false;
				}
Ejemplo n.º 26
0
				public static bool PeekAndParse(
					CharStream stream, out AST.INode expression)
				{
					if (stream.PeekNextRange(2) == "{{")
					{
						expression = Expander.Parse(stream);
						return true;
					}

					expression = null;
					return false;
				}
Ejemplo n.º 27
0
 public void CharStream_CanProperlyOrderCharacters(string input)
 {
     var cs = new CharStream(input);
     foreach (var c in input)
     {
         var found = cs.Read();
         Assert.True(found.HasValue);
         Assert.Equal(c, found.Value);
     }
     var end = cs.Read();
     Assert.True(!end.HasValue);
 }
Ejemplo n.º 28
0
					public static bool PeekAndParse(
						CharStream stream, out AST.INode expression)
					{
						if (stream.PeekReg(@"[$@]?\w+(\.\w+)*::"))
						{
							expression = Attribute.Parse(stream);
							return true;
						}
						
						expression = null;
						return false;
					}
Ejemplo n.º 29
0
				public static bool PeekAndParse(
					CharStream stream, out AST.INode literal)
				{
					if (!Literal.Peek(stream))
					{
						literal = null;
						return false;
					}

					literal = Literal.Parse(stream);
					return true;
				}
Ejemplo n.º 30
0
					public static bool PeekAndParse(
						CharStream stream, out AST.INode variable)
					{
						if (!Variable.Peek(stream))
						{
							variable = null;
							return false;
						}

						variable = Variable.Parse(stream);
						return true;
					}