public static BaseExpression Parse(ParsingEnvironment env, string input)
        {
            var vistor = new MyExpressionParser()
            {
                _environment = env
            };
            ICharStream     stream = CharStreams.fromString(input);
            ExpressionLexer lexer  = new ExpressionLexer(stream);

            lexer.AddErrorListener(vistor);
            ITokenStream tokens = new CommonTokenStream(lexer);
            var          parser = new ExpressionParser(tokens);

            parser.AddErrorListener(vistor);
            parser.BuildParseTree = true;
            var tree = parser.completeExpression();
            // TODO: the default error listens write to the console
            BaseExpression result = vistor.Visit(tree);

            if (result is null)
            {
                // Probably missed an override of ExpressionBaseVisitor if this is thrown
                throw new Exception("Programming error, no expression returned.");
            }
            return(result);
        }
Exemple #2
0
        public static BaseExpression Parse(ParsingEnvironment env, XElement node)
        {
            switch (node.Name.LocalName)
            {
            case "ValueFromFirstFormThatExists":
                return(new ValueFromFirstFormThatExistsExpression(env, node));

            case "SelectBasedOnStatus":
                return(new SelectBasedOnStatusExpression(env, node));

            case "Form8949Lines":
                return(new Form8949LinesLiteralExpression(node));

            default:
                throw new FileLoadException(node, "Unknown node type: " + node.Name);
            }
        }
        public static BaseExpression Parse(ParsingEnvironment env, XElement node, string attributeName)
        {
            var calcStr  = node.Attribute(attributeName)?.Value;
            var calcNode = node.Elements().ToArray();

            //make sure there is at most one Calc definition
            if (calcStr is null && calcNode.Length == 0)
            {
                return(null);
            }
            if (calcStr is object && calcNode.Length > 0)
            {
                throw new FileLoadException(node, $"Line contains both and {attributeName} attribute and a {attributeName} node.");
            }

            // do some light validation
            if (calcStr is not null)
            {
                if (string.IsNullOrWhiteSpace(calcStr))
                {
                    if (calcStr is null)
                    {
                        return(null);
                    }
                    throw new FileLoadException(node, "Empty expression.");
                }
            }
            else if (calcNode.Length > 1)
            {
                throw new FileLoadException(calcNode[1], $"Multiple {attributeName} sub-elements.");
            }

            try
            {
                return(calcStr is not null?Parse(env, calcStr) : XmlExpressionParser.Parse(env, calcNode[0]));
            }
            catch (Exception ex)
            {
                throw new FileLoadException(node, $"Failed to parse {attributeName}.", ex);
            }
        }