public static bool PeekAndParse(CharStream cs, out FTL.AST.Pattern pattern) { // A quoted-pattern is the easiest to detect, so we'll try that first if (cs.PeekNext() == '"') { pattern = ParseQuoted(cs); return(true); } // it might still be an unquoted-pattern, but this is trickier to detect // first let's try to detect a placeable and block-text if (Placeable.Peek(cs) || AnyText.PeekBlockText(cs)) { pattern = ParseUnquoted(cs); return(true); } // if not any of the above, the only thing left is unquoted-text int bufferPos = cs.Position; WhiteSpace.Parse(cs); char next = cs.PeekNext(); cs.Rewind(bufferPos); if (!CharStream.IsEOF(next) && !CharStream.IsNL(next)) { pattern = ParseUnquoted(cs); return(true); } pattern = null; return(false); }
public void UnquotedTextTests() { Assert.IsNotNull(AnyText.ParseUnquoted(NCS("this is fine"))); Assert.IsNotNull(AnyText.ParseUnquoted(NCS("can be almost anything\nanother line"))); Assert.IsNotNull(AnyText.ParseUnquoted(NCS("curly brackets to have be escaped like this: \\{"))); CharStream cs = NCS("otherwise {it will stop"); Assert.IsNotNull(AnyText.ParseUnquoted(cs)); Assert.AreEqual("{it will stop", cs.ReadUntilEnd()); }
public void BlockTextTests() { L20n.FTL.AST.INode result; // good examples Assert.IsTrue(AnyText.PeekAndParseBlock(NCS(@" | this blocktext | is fine" ), out result)); var cs = NCS(@" | this blocktext is also fine hello" ); Assert.IsTrue(AnyText.PeekAndParseBlock(cs, out result)); Assert.AreEqual(@" hello" , cs.ReadUntilEnd()); // bad examples // newline required Assert.IsFalse(AnyText.PeekAndParseBlock(NCS(""), out result)); // | required Assert.IsFalse(AnyText.PeekAndParseBlock(NCS(@" "), out result)); }
// '"' (placeable | quoted-text)* '"' public static FTL.AST.Pattern ParseQuoted(CharStream cs) { cs.SkipCharacter('"'); FTL.AST.Pattern pattern = new FTL.AST.Pattern(true); FTL.AST.INode child; while (cs.PeekNext() != '"') { if (Placeable.PeekAndParse(cs, out child)) { pattern.AddChild(child); continue; } // it's not a placeable, and we haven't seen a quote, // so it must be a quoted-text child = AnyText.ParseQuoted(cs); pattern.AddChild(child); } cs.SkipCharacter('"'); return(pattern); }
private static FTL.AST.INode ParseUnquotedChild(CharStream cs) { FTL.AST.INode child; if (Placeable.PeekAndParse(cs, out child)) { return(child); } if (AnyText.PeekAndParseBlock(cs, out child)) { return(child); } // as long as we don't have a newline char, // we'll assume it's an unquoted-child if (AnyText.PeekUnquoted(cs)) { return(AnyText.ParseUnquoted(cs)); } // return null if no child could be found return(null); }