示例#1
0
        private void readFile(string filename)
        {
            string json = File.ReadAllText(filename);

            Rules.AddRange(Rule.FromJson(json));

            List <Symbol> temp = new List <Symbol>();

            // 找出所有的符号
            foreach (var rule in Rules)
            {
                temp.Add(rule.Left);
                temp.AddRange(rule.Right);
            }
            Symbols.AddRange(temp.Distinct().ToList());
            foreach (var sym in Symbols)
            {
                if (sym.Type == "N")
                {
                    N_Symbols.Add(sym);
                }
                if (sym.Type == "V")
                {
                    V_Symbols.Add(sym);
                }
            }
            Symbols.Add(D);
        }
        private async Task GetSymbols()
        {
            IsLoadingSymbols = true;

            try
            {
                var results = await ExchangeService.GetSymbolsAsync(userAccount.Exchange, new CancellationToken()).ConfigureAwait(true);

                Func <Symbol, string, Symbol> f = ((s, p) =>
                {
                    s.IsFavourite = true;
                    return(s);
                });

                (from s in results join p in userAccount.Preferences.FavouriteSymbols on s.ExchangeSymbol equals p select f(s, p)).ToList();

                Symbols.Clear();
                Symbols.AddRange(results);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), Category.Exception, Priority.Low);
                Dialog.ShowException(ex);
            }
            finally
            {
                IsLoadingSymbols = false;
            }
        }
示例#3
0
 public Grammar(Grammar grammar)
 {
     foreach (var rule in grammar.Rules)
     {
         Rules.Add(new Rule(rule));
     }
     Symbols.AddRange(grammar.Symbols);
 }
示例#4
0
 public Rule(string name, params Symbol[] symbols)
 {
     Name = name;
     Symbols.AddRange(symbols);
 }
示例#5
0
 public SymbolOverload(params IMemberDefinition[] members)
     : this()
 {
     Symbols.AddRange(members.Select(x => new SymbolDefinition(x)));
 }
示例#6
0
 public SymbolOverload(params SymbolDefinition[] members)
     : this()
 {
     Symbols.AddRange(members);
 }
示例#7
0
 public Word(List <ISymbol> word)
 {
     Symbols.AddRange(word);
     word.ForEach(x => stringWord += x.Character);
 }
示例#8
0
 public void Add(List <Symbol> symbols)
 {
     Symbols.AddRange(symbols);
 }
示例#9
0
        // generates the rule logic inside the method body
        private string GenerateProductionRuleCode(Rules rules, int index, int indent)
        {
            Rule          r              = rules[index];
            Symbols       firsts         = null;
            Symbols       firstsExtended = null;
            StringBuilder sb             = new StringBuilder();
            string        Indent         = IndentTabs(indent);

            switch (r.Type)
            {
            case RuleType.Terminal:
                // expecting terminal, so scan it.
                sb.AppendLine(Indent + "tok = scanner.Scan(TokenType." + r.Symbol.Name + ");" + Helper.AddComment("Terminal Rule: " + r.Symbol.Name));
                sb.AppendLine(Indent + "n = node.CreateNode(tok, tok.ToString() );");
                sb.AppendLine(Indent + "node.Token.UpdateRange(tok);");
                sb.AppendLine(Indent + "node.Nodes.Add(n);");
                sb.AppendLine(Indent + "if (tok.Type != TokenType." + r.Symbol.Name + ") {");
                sb.AppendLine(Indent + IndentString + "tree.Errors.Add(new ParseError(\"Unexpected token '\" + tok.Text.Replace(\"\\n\", \"\") + \"' found. Expected \" + TokenType." + r.Symbol.Name + ".ToString(), 0x1001, tok));");
                sb.AppendLine(Indent + IndentString + "return;");
                sb.AppendLine(Indent + "}");
                break;

            case RuleType.NonTerminal:
                sb.AppendLine(Indent + "Parse" + r.Symbol.Name + "(node);" + Helper.AddComment("NonTerminal Rule: " + r.Symbol.Name));
                break;

            case RuleType.Concat:

                for (int i = 0; i < r.Rules.Count; i++)
                {
                    sb.AppendLine();
                    sb.AppendLine(Indent + Helper.AddComment("Concat Rule"));
                    sb.Append(GenerateProductionRuleCode(r.Rules, i, indent));
                }
                break;

            case RuleType.ZeroOrMore:
                firsts         = r.GetFirstTerminals();
                firstsExtended = CollectExpectedTokens(rules, index + 1);
                firstsExtended.AddRange(firsts);
                sb.Append(Indent + "tok = scanner.LookAhead(");
                AppendTokenList(firsts, sb);
                sb.AppendLine(");" + Helper.AddComment("ZeroOrMore Rule"));

                sb.Append(Indent + "while (");
                AppendTokenCondition(firsts, sb, Indent);
                sb.AppendLine(")");
                sb.AppendLine(Indent + "{");

                for (int i = 0; i < r.Rules.Count; i++)
                {
                    sb.Append(GenerateProductionRuleCode(r.Rules, i, indent + 1));
                }

                sb.Append(Indent + "tok = scanner.LookAhead(");
                AppendTokenList(firstsExtended, sb);
                sb.AppendLine(");" + Helper.AddComment("ZeroOrMore Rule"));
                sb.AppendLine(Indent + "}");
                break;

            case RuleType.OneOrMore:
                sb.AppendLine(Indent + "found = false;");
                sb.AppendLine(Indent + "do {" + Helper.AddComment("OneOrMore Rule"));

                for (int i = 0; i < r.Rules.Count; i++)
                {
                    sb.Append(GenerateProductionRuleCode(r.Rules, i, indent + 1));
                }

                firsts         = r.GetFirstTerminals();
                firstsExtended = CollectExpectedTokens(rules, index + 1);
                firstsExtended.AddRange(firsts);
                sb.AppendLine(Indent + IndentString + "if(!found) {");
                sb.Append(Indent + IndentString + IndentString + "tok = scanner.LookAhead(");
                AppendTokenList(firsts, sb);
                sb.AppendLine(");" + Helper.AddComment("OneOrMore Rule"));
                sb.AppendLine(Indent + IndentString + "found = true;");
                sb.AppendLine(Indent + IndentString + "} else {");
                sb.Append(Indent + IndentString + IndentString + "tok = scanner.LookAhead(");
                AppendTokenList(firstsExtended, sb);
                sb.AppendLine(");" + Helper.AddComment("OneOrMore Rule"));
                sb.AppendLine(Indent + IndentString + "}");
                sb.Append(Indent + "} while (");
                AppendTokenCondition(firsts, sb, Indent);
                sb.AppendLine(");" + Helper.AddComment("OneOrMore Rule"));
                break;

            case RuleType.Option:
                firsts = r.GetFirstTerminals();
                sb.Append(Indent + "tok = scanner.LookAhead(");
                AppendTokenList(firsts, sb);
                sb.AppendLine(");" + Helper.AddComment("Option Rule"));

                sb.Append(Indent + "if (");
                AppendTokenCondition(firsts, sb, Indent);
                sb.AppendLine(")");
                sb.AppendLine(Indent + "{");

                for (int i = 0; i < r.Rules.Count; i++)
                {
                    sb.Append(GenerateProductionRuleCode(r.Rules, i, indent + 1));
                }
                sb.AppendLine(Indent + "}");
                break;

            case RuleType.Choice:
                firsts = r.GetFirstTerminals();
                sb.Append(Indent + "tok = scanner.LookAhead(");
                var tokens = new List <string>();
                AppendTokenList(firsts, sb, tokens);
                string expectedTokens;
                if (tokens.Count == 1)
                {
                    expectedTokens = tokens[0];
                }
                else if (tokens.Count == 2)
                {
                    expectedTokens = tokens[0] + " or " + tokens[1];
                }
                else
                {
                    expectedTokens  = string.Join(", ", tokens.GetRange(0, tokens.Count - 1).ToArray());
                    expectedTokens += ", or " + tokens[tokens.Count - 1];
                }
                sb.AppendLine(");" + Helper.AddComment("Choice Rule"));

                sb.AppendLine(Indent + "switch (tok.Type)");
                sb.AppendLine(Indent + "{" + Helper.AddComment("Choice Rule"));
                for (int i = 0; i < r.Rules.Count; i++)
                {
                    foreach (TerminalSymbol s in r.Rules[i].GetFirstTerminals())
                    {
                        sb.AppendLine(Indent + IndentString + "case TokenType." + s.Name + ":");
                    }
                    sb.Append(GenerateProductionRuleCode(r.Rules, i, indent + 2));
                    sb.AppendLine(Indent + IndentString + IndentString + "break;");
                }
                sb.AppendLine(Indent + IndentString + "default:");
                sb.AppendLine(Indent + IndentString + IndentString + "tree.Errors.Add(new ParseError(\"Unexpected token '\" + tok.Text.Replace(\"\\n\", \"\") + \"' found. Expected " + expectedTokens + ".\", 0x0002, tok));");
                sb.AppendLine(Indent + IndentString + IndentString + "break;");
                sb.AppendLine(Indent + "}" + Helper.AddComment("Choice Rule"));
                break;

            default:
                break;
            }
            return(sb.ToString());
        }