/// <summary> /// Parse the given line. /// </summary> /// <param name="line">The line to parse.</param> /// <returns>The parsed probability.</returns> public ParsedProbability Parse(String line) { ParsedProbability result = new ParsedProbability(); SimpleParser parser = new SimpleParser(line); parser.EatWhiteSpace(); if (!parser.LookAhead("P(", true)) { throw new EncogError("Bayes table lines must start with P("); } parser.Advance(2); // handle base AddEvents(parser, result.BaseEvents, "|,)=[]"); // handle conditions if (parser.Peek() == '|') { parser.Advance(); AddEvents(parser, result.GivenEvents, ",)=[]"); } if (parser.Peek() != ')') { throw new BayesianError("Probability not properly terminated."); } return(result); }
/// <summary> /// Read the specified parameter. /// </summary> /// <param name="parser">The parser to use.</param> /// <returns>The parsed parameter.</returns> private ParamTemplate ReadParam(SimpleParser parser) { var result = new ParamTemplate(); if (!parser.LookAhead("{", true)) { throw new EACompileError("Expected {"); } parser.Advance(); bool done = false; var buffer = new StringBuilder(); while (!done) { if (parser.Peek() == '}') { done = true; parser.Advance(); } else if (parser.Peek() == '{') { throw new EACompileError("Unexpected {"); } else if (parser.Peek() == '{') { done = true; parser.Advance(); } else if (parser.Peek() == ',') { result.AddType(buffer.ToString().Trim().ToLower()); parser.Advance(); buffer.Length = 0; } else { buffer.Append(parser.ReadChar()); } } String s = buffer.ToString().Trim(); if (s.Length > 0) { result.AddType(s); } return(result); }
/// <summary> /// Parse a value. /// </summary> /// /// <param name="parser">The parser to use.</param> /// <returns>The newly parsed value.</returns> private static String ParseValue(SimpleParser parser) { bool quoted = false; var str = new StringBuilder(); parser.EatWhiteSpace(); if (parser.Peek() == '\"') { quoted = true; parser.Advance(); } while (!parser.EOL()) { if (parser.Peek() == '\"') { if (quoted) { parser.Advance(); if (parser.Peek() == '\"') { str.Append(parser.ReadChar()); } else { break; } } else { str.Append(parser.ReadChar()); } } else if (!quoted && (parser.IsWhiteSpace() || (parser.Peek() == ','))) { break; } else { str.Append(parser.ReadChar()); } } return(str.ToString()); }
/// <summary> /// Parse a line with space separators. /// </summary> /// <param name="line">The line to parse.</param> /// <returns>The list of items from the line.</returns> private static List<String> ParseSpaceSep(String line) { var result = new List<String>(); var parse = new SimpleParser(line); while (!parse.EOL()) { result.Add(parse.Peek() == '\"' ? parse.ReadQuotedString() : parse.ReadToWhiteSpace()); parse.EatWhiteSpace(); } return result; }
/// <summary> /// Parse a line with space separators. /// </summary> /// <param name="line">The line to parse.</param> /// <returns>The list of items from the line.</returns> private static List <String> ParseSpaceSep(String line) { var result = new List <String>(); var parse = new SimpleParser(line); while (!parse.EOL()) { result.Add(parse.Peek() == '\"' ? parse.ReadQuotedString() : parse.ReadToWhiteSpace()); parse.EatWhiteSpace(); } return(result); }
/// <summary> /// Construct a basic template object. /// </summary> /// <param name="thePrecedence">The precedence.</param> /// <param name="theSignature">The opcode signature.</param> /// <param name="theType">The opcode type.</param> /// <param name="isVariable">True, if this opcode is a variable.</param> /// <param name="theDataSize">The data size kept for this opcode.</param> /// <param name="theEvaluate">The evaluator delegate.</param> /// <param name="theIsPossibleReturnType">The return type delegate.</param> /// <param name="theRandomize">The randomizer delegate.</param> public BasicTemplate(int thePrecedence, String theSignature, NodeType theType, bool isVariable, int theDataSize, DelEvaluate theEvaluate, DelIsPossibleReturnType theIsPossibleReturnType, DelRandomize theRandomize) { _precedence = thePrecedence; _signature = theSignature; _varValue = isVariable; _dataSize = theDataSize; _nodeType = theType; _delEvaluate = theEvaluate; _delIsPossibleReturnType = theIsPossibleReturnType; _delRandomize = theRandomize; if (theSignature.Trim().Equals("(")) { // special case, we add a left-paren for the shunting yard alg. _name = theSignature; _returnValue = null; } else { // non-special case, find the name of the function/operator var parser = new SimpleParser(theSignature); bool pass = false; parser.EatWhiteSpace(); _name = parser.ReadToChars("(").Trim(); parser.Advance(); bool done = false; while (!done) { if (parser.Peek() == ')') { parser.Advance(); done = true; } else if (parser.Peek() == ':') { parser.Advance(); pass = true; } else if (parser.Peek() == '{') { ParamTemplate temp = ReadParam(parser); temp.PassThrough = pass; pass = false; _param.Add(temp); } else { parser.Advance(); if (parser.EOL()) { throw new EncogError("Invalid opcode template."); } } } // get the return type parser.EatWhiteSpace(); if (!parser.LookAhead(":", true)) { throw new EACompileError("Return type not specified."); } parser.Advance(); parser.EatWhiteSpace(); _returnValue = ReadParam(parser); } }
/// <summary> /// Add events, as they are pased. /// </summary> /// <param name="parser">The parser.</param> /// <param name="results">The events found.</param> /// <param name="delim">The delimiter to use.</param> private void AddEvents(SimpleParser parser, IList <ParsedEvent> results, String delim) { bool done = false; StringBuilder l = new StringBuilder(); while (!done && !parser.EOL()) { char ch = parser.Peek(); if (delim.IndexOf(ch) != -1) { if (ch == ')' || ch == '|') { done = true; } ParsedEvent parsedEvent; // deal with a value specified by + or - if (l.Length > 0 && l[0] == '+') { String l2 = l.ToString().Substring(1); parsedEvent = new ParsedEvent(l2.Trim()); parsedEvent.Value = "true"; } else if (l.Length > 0 && l[0] == '-') { String l2 = l.ToString().Substring(1); parsedEvent = new ParsedEvent(l2.Trim()); parsedEvent.Value = "false"; } else { String l2 = l.ToString(); parsedEvent = new ParsedEvent(l2.Trim()); } // parse choices if (ch == '[') { parser.Advance(); int index = 0; while (ch != ']' && !parser.EOL()) { String labelName = parser.ReadToChars(":,]"); if (parser.Peek() == ':') { parser.Advance(); parser.EatWhiteSpace(); double min = double.Parse(parser.ReadToWhiteSpace()); parser.EatWhiteSpace(); if (!parser.LookAhead("to", true)) { throw new BayesianError("Expected \"to\" in probability choice range."); } parser.Advance(2); double max = CSVFormat.EgFormat.Parse(parser.ReadToChars(",]")); parsedEvent.ChoiceList.Add(new ParsedChoice(labelName, min, max)); } else { parsedEvent.ChoiceList.Add(new ParsedChoice(labelName, index++)); } parser.EatWhiteSpace(); ch = parser.Peek(); if (ch == ',') { parser.Advance(); } } } // deal with a value specified by = if (parser.Peek() == '=') { parser.ReadChar(); String value = parser.ReadToChars(delim); // BayesianEvent evt = this.network.getEvent(parsedEvent.getLabel()); parsedEvent.Value = value; } if (ch == ',') { parser.Advance(); } if (ch == ']') { parser.Advance(); } if (parsedEvent.Label.Length > 0) { results.Add(parsedEvent); } l.Length = 0; } else { parser.Advance(); l.Append(ch); } } }
public ProgramNode Parse(String expression) { this.parser = new SimpleParser(expression); this.unary = true; while (!parser.EOL()) { parser.EatWhiteSpace(); char ch = parser.Peek(); if (ch == '.' || char.IsDigit(ch)) { HandleNumeric(); this.unary = false; } else if (char.IsLetter(ch)) { handleAlpha(false); this.unary = false; } else if (ch == '(') { this.parser.Advance(); this.functionStack.Push(LEFT_PAREN); this.unary = true; } else if (ch == ')') { handleRightParen(); this.unary = false; } else if ("<>^*/+-=&|".IndexOf(ch) != -1) { handleSymbol(); this.unary = true; } else if (ch == '\"') { handleString(); this.unary = false; } else if (ch == ',') { HandleFunctionSeparator(); this.unary = true; } else { throw new EACompileError("Unparsable character: " + ch); } } // pop off any functions still on the stack while (this.functionStack.Count > 0) { IProgramExtensionTemplate f = this.functionStack.Pop(); OutputQueue(f); } // were there no operators? if (this.rootNode == null) { this.rootNode = this.outputStack.Pop(); } return(this.rootNode); }
// Subjects the SimpleParser class to a series of tests, returning True if it passes. public static bool ExecuteUnitTest() { // Note that the collection of tests below is NOT exhaustive, but they do give an // indication of the basic functionality of the SimpleParser class. They should be // helpful in validating after any future code refactoring (e.g. SimpleParser could // be rewritten to use exclusively base-1 indexing internally, and this test might // assist in verifying the various +1's and -1's were tacked on correctly. // Some of these tests may be repetitive.. I added things in several times without // checking to see if they superceeded parts that were already written. // But hey, it's not like we need to worry about having *too many* tests here! SimpleParser p = default(SimpleParser); bool caught = false; UTO("TESTING THE SIMPLEPARSER CLASS ---"); UTO("Testing constructors..."); p = new SimpleParser(); if (p.Text != "" || p.Cursor != 0) { return(false); } p = new SimpleParser("Hello"); if (p.Text != "Hello" || p.Cursor != 0) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing basic functionality..."); p.Text = "abCdeFghijklmnop123aa1123"; if (p.Extract("C", "F") != "de") { return(false); } if (p.Cursor != 6) { return(false); } p.SeekNext("i"); if (p.Cursor != 9) { return(false); } p.SeekPrev("F"); if (p.Cursor != 5) { return(false); } if (p.Peek("f", "i") != "gh") { return(false); } if (p.Cursor != 5) { return(false); } if (p.SeekAndExtract("123", "1", "3") != "12") { return(false); } if (p.Cursor != 25) { return(false); } if (p.TrySeekPrev("Z")) { return(false); } if (p.Cursor != 25) { return(false); } // Some testing of SeekPrev on the right bound p.Cursor = p.Text.Length; if (!p.TrySeekPrev("3")) { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } p.Text += "Y"; p.Cursor = p.Text.Length; if (!p.TrySeekPrev("Y")) { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } // Testing of small string p.Text = "A"; if (!p.TrySeekNext("A")) { return(false); } if (!p.TrySeekPrev("A")) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing case insensitivity..."); p.Text = "abc123CdeFghi"; p.CaseSensitive = true; if (p.Extract("C", "F") != "de") { return(false); } p.CaseSensitive = false; p.Cursor = 0; if (p.Extract("C", "F") != "123Cde") { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing cursor property edges..."); p.Text = "abcdef"; p.Cursor = 0; p.Cursor = p.Text.Length - 1; if (p.TryExtract("a", "b") != "") { return(false); } if (p.Cursor != p.Text.Length - 1) { return(false); } p.Cursor += 1; // cursor is allowed to be to the right of the last character UTO("Passed" + Environment.NewLine); UTO("Testing cursor right edge exception..."); p.Text = "abcdef"; caught = false; try { p.Cursor = p.Text.Length + 1; } catch (ArgumentOutOfRangeException) { caught = true; } if (!caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing cursor left edge exception..."); p.Text = "abcdef"; caught = false; try { p.Cursor = -1; } catch (ArgumentOutOfRangeException) { caught = true; } if (!caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing that SeekNext generates exception if called when cursor is at right edge of text..."); p.Text = "abc"; p.Cursor = 3; caught = false; try { p.SeekNext("a"); } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (!caught) { return(false); } // Now make sure it doesn't throw and exception using the other method p.Cursor = 3; caught = false; try { if (p.TrySeekNext("a") != false) { return(false); } } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing that SeekPrev generates exception if called when cursor is at left edge of text..."); p.Text = "abc"; p.Cursor = 0; caught = false; try { p.SeekPrev("a"); } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (!caught) { return(false); } // Now make sure it doesn't throw and exception using the other method p.Cursor = 0; caught = false; try { if (p.TrySeekPrev("a") != false) { return(false); } } catch (SimpleParser.SymbolNotFoundException) { caught = true; } if (caught) { return(false); } UTO("Passed" + Environment.NewLine); UTO("Testing extraction with zero-length arguments..."); p.Text = "abc"; p.Cursor = 0; if (p.Extract("", "a") != "") { return(false); } p.Cursor = 0; if (p.Extract("", "abc") != "") { return(false); } p.Cursor = 0; if (p.Extract("", "b") != "a") { return(false); } p.Cursor = 0; if (p.Extract("", "c") != "ab") { return(false); } p.Cursor = 0; if (p.Extract("", "") != "abc") { return(false); } p.Text = ""; if (p.Extract("", "") != "") { return(false); } p.Cursor = 0; if (p.CanExtract("a", "")) { return(false); } UTO("Passed" + Environment.NewLine); UTO("ALL TESTS PASSED!"); return(true); }