Example #1
0
 string MultiParserActionGenerate(Symbol symbol)
 {
     var template = @"{0}result = {1};
                 if ({0}result.parsed)
                 {{
                     {2}
                     {0}results.Push(new Result(null, ""{3}"", {0}value));
                     return {0}result;
                 }}";
     return string.Format(template, prefix, symbol.Expression.Generate(0), MultiActionGenerate(symbol), symbol.Type);
 }
Example #2
0
        string MultiActionGenerate(Symbol symbol)
        {
            if (symbol.Expression.Action == null)
            {
                if (symbol.Expression is Rule)
                    return string.Format("var {0}value = {0}actionResults.Pop().value;", prefix);
                return string.Format("var {0}value = input.Substring({0}state.index, {0}result.index - {0}state.index);", prefix);
            }

            var template = @"Func<{0}> {1}action = ({2}) => {3};
                            var {1}value = ({4}){1}action({5});";
            var labels = GetLabel(symbol.Expression);

            if (!labels.Any())
                return string.Format(template, symbol.Type, prefix, "", symbol.Expression.Action, symbol.Type, "");

            var types = string.Join(", ", labels.Select(x => x.Type)) + ", " + symbol.Type;
            var parameters = string.Join(", ", labels.Select(x => x.Type + " " + x.Label));
            var args = string.Join(", ", labels.Select(x => prefix + "actionResults.Single(x => x.label == \"" + x.Label + "\").value"));
            return string.Format(template, types, prefix, parameters, symbol.Expression.Action, symbol.Type, args);
        }