示例#1
0
        public void TestExceptions()
        {
            const string NOT_HERE = "Should've thrown an Exception instead of ending up here.";

            try {
                SimpleParser.Build(Parser.ParseString("{not-node}"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: () list expected while building a Node.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("(not a node because has lots of expressions)"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: Node's () list must contain from 1 to 3 element within.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("()"));                 // not a node because not enough expressions within
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: Node's () list must contain from 1 to 3 element within.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("((name))"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: Node's name must be a keyword.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("(n [] {})"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: {} list excepted while building Node's attributes.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("(n {(n)})"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: attribute name must be a keyword.");
            }

            try {
                SimpleParser.Build(Parser.ParseString("(n {} {})"));
                Assert.Fail(NOT_HERE);
            } catch (Exception e) {
                Assert.AreEqual(e.GetType(), typeof(InvalidDataException));
                Assert.AreEqual(e.Message, "Syntax error: Node's child or children cannot be represented as {} list.");
            }
        }
示例#2
0
        public void TestBuilding()
        {
            var x = Parser.ParseString("(node {attr (node 3.14) attr2 (node [\"6\" \"7\"])} [true false null])");
            var s = SimpleParser.Build(x);
            var n = s as Node;

            AssertNode(n, "node", 2, 3);

            var attr     = n.Attributes["attr"];
            var attrNode = attr as Node;

            AssertNode(attrNode, "node", 0, 1);
            AssertNumberLiteral(attrNode.Children[0] as NumberLiteral, 3, 14);

            var attr2     = n.Attributes["attr2"];
            var attr2Node = attr2 as Node;

            AssertNode(attr2Node, "node", 0, 2);
            AssertStringLiteral(attr2Node.Children[0] as StringLiteral, "6");
            AssertStringLiteral(attr2Node.Children[1] as StringLiteral, "7");

            AssertBooleanLiteral(n.Children[0] as BooleanLiteral, true);
            AssertBooleanLiteral(n.Children[1] as BooleanLiteral, false);
            Assert.AreEqual(n.Children[2].GetType(), typeof(NullLiteral));

            // a few other cases

            var fn = TestHelper.MakeTempFile("(node (node {a 1 b 2}))");

            s = SimpleParser.Parse(fn);
            TestHelper.DeleteTempFile(fn);
            n = s as Node;
            AssertNode(n, "node", 0, 1);

            n = n.Children[0] as Node;
            AssertNode(n, "node", 2, 0);
            AssertNumberLiteral(n.Attributes["a"] as NumberLiteral, 1, 0);
        }
示例#3
0
文件: Condition.cs 项目: Tkachov/sdf
        private static ValueCondition ParseOperator(string condition, string operatorName, int index)
        {
            string attributeName = null;

            if (index > 0)
            {
                attributeName = condition.Substring(0, index);
                if (!attributeName.StartsWith("@"))
                {
                    throw new InvalidDataException("Invalid attribute name given (does not start with an @).");
                }

                attributeName = attributeName.Substring(1);                 // remove @
            }

            var value = condition.Substring(index + operatorName.Length);
            var expr  = Parser.ParseString(value);

            if (!(expr is LiteralExpression))
            {
                throw new InvalidDataException("Argument of an operator in value condition is not a literal.");
            }

            var sdf = SimpleParser.Build(expr);
            OperatorCondition operatorCondition;

            switch (operatorName)
            {
            case "=":
            case "!=":
                // common operators
                operatorCondition = new CommonOperatorCondition(operatorName, sdf);
                break;

            case ">":
            case "<":
            case ">=":
            case "<=":
                // number operators
                if (!(sdf is NumberLiteral))
                {
                    throw new InvalidDataException("Cannot apply a number operator in value condition to something but a number literal.");
                }

                operatorCondition = new NumberOperatorCondition(operatorName, (NumberLiteral)sdf);
                break;

            case "~=":
            case "^=":
            case "$=":
            case "!~=":
            case "!^=":
            case "!$=":
                // string operators
                if (!(sdf is StringLiteral))
                {
                    throw new InvalidDataException("Cannot apply a string operator in value condition to something but a string literal.");
                }

                operatorCondition = new StringOperatorCondition(operatorName, (StringLiteral)sdf);
                break;

            default:
                throw new InvalidDataException("Unknown operator given.");
            }

            if (attributeName != null)
            {
                return(new AttributeValueCondition(attributeName, operatorCondition));
            }

            return(new NodeValueCondition(operatorCondition));
        }
示例#4
0
文件: TestHelper.cs 项目: Tkachov/sdf
        // parsing

        internal static SDF ParseString(string s)
        {
            return(SimpleParser.Build(Parser.ParseString(s)));
        }