예제 #1
0
        public void NumericSingleTokenTests()
        {
            Scanner lexer;

            lexer = new Scanner("1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("1234", lexer.IsNextString()));

            lexer = new Scanner("-1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("-1234", lexer.IsNextString()));

            lexer = new Scanner("+1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("+1234", lexer.IsNextString()));

            lexer = new Scanner("1234.1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("1234.1234", lexer.IsNextString()));

            lexer = new Scanner(".1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare(".1234", lexer.IsNextString()));

            lexer = new Scanner("1234.", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("1234.", lexer.IsNextString()));
            lexer = new Scanner("0x1234", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("0x1234", lexer.IsNextString()));
            lexer = new Scanner("0X1234abcd", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("0X1234abcd", lexer.IsNextString()));
            lexer = new Scanner("0x1234ABCD", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            Assert.Equal(0, String.Compare("0x1234ABCD", lexer.IsNextString()));
        }
예제 #2
0
        public void ItemListTests()
        {
            Scanner lexer;

            lexer = new Scanner("@(foo)", ParserOptions.AllowProperties);
            Assert.IsFalse(lexer.Advance());
            Assert.IsTrue(String.Compare(lexer.GetErrorResource(), "ItemListNotAllowedInThisConditional") == 0);

            lexer = new Scanner("1234 '@(foo)'", ParserOptions.AllowProperties);
            Assert.IsTrue(lexer.Advance());
            Assert.IsFalse(lexer.Advance());
            Assert.IsTrue(String.Compare(lexer.GetErrorResource(), "ItemListNotAllowedInThisConditional") == 0);

            lexer = new Scanner("'1234 @(foo)'", ParserOptions.AllowProperties);
            Assert.IsFalse(lexer.Advance());
            Assert.IsTrue(String.Compare(lexer.GetErrorResource(), "ItemListNotAllowedInThisConditional") == 0);
        }
예제 #3
0
        public MatchCollection Matches(Scanner scanner)
        {
            scanner.ThrowIfNull("scanner");
            var  matches = new MatchCollection();
            bool eof     = scanner.IsEof;

            while (!eof)
            {
                GrammarMatch match = Match(scanner);
                if (match.Success)
                {
                    matches.AddRange(match.Matches);
                    eof = scanner.IsEof;
                }
                else
                {
                    eof = scanner.Advance(1) < 0;
                }
            }
            return(matches);
        }
예제 #4
0
        public void ComplexTests1()
        {
            Scanner lexer = new Scanner("'String with a $(Property) inside'", ParserOptions.AllowAll);

            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.Equal(0, String.Compare("String with a $(Property) inside", lexer.IsNextString()));

            lexer = new Scanner("'String with an embedded \\' in it'", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            //          Assert.AreEqual(String.Compare("String with an embedded ' in it", lexer.IsNextString()), 0);

            lexer = new Scanner("'String with a $(Property) inside'", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.Equal(0, String.Compare("String with a $(Property) inside", lexer.IsNextString()));

            lexer = new Scanner("@(list, ' ')", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.Equal(0, String.Compare("@(list, ' ')", lexer.IsNextString()));

            lexer = new Scanner("@(files->'%(Filename)')", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.Equal(0, String.Compare("@(files->'%(Filename)')", lexer.IsNextString()));
        }
예제 #5
0
        public void ComplexTests2()
        {
            Scanner lexer = new Scanner("1234", ParserOptions.AllowAll);

            Assert.True(lexer.Advance());

            lexer = new Scanner("'abc-efg'==$(foo)", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.String));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.EqualTo));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.Property));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.EndOfInput));

            lexer = new Scanner("$(debug)!=true", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Property));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.NotEqualTo));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.String));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.EndOfInput));

            lexer = new Scanner("$(VERSION)<5", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Property));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.LessThan));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.Numeric));
            lexer.Advance();
            Assert.True(lexer.IsNext(Token.TokenType.EndOfInput));
        }
예제 #6
0
        public void PropsStringsAndBooleanSingleTokenTests()
        {
            Scanner lexer = new Scanner("$(foo)", ParserOptions.AllowAll);

            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Property));
            lexer = new Scanner("@(foo)", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.ItemList));
            lexer = new Scanner("abcde", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.String));
            Assert.Equal(0, String.Compare("abcde", lexer.IsNextString()));

            lexer = new Scanner("'abc-efg'", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.String));
            Assert.Equal(0, String.Compare("abc-efg", lexer.IsNextString()));

            lexer = new Scanner("and", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.And));
            Assert.Equal(0, String.Compare("and", lexer.IsNextString()));
            lexer = new Scanner("or", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Or));
            Assert.Equal(0, String.Compare("or", lexer.IsNextString()));
            lexer = new Scanner("AnD", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.And));
            Assert.Equal(0, String.Compare(Token.And.String, lexer.IsNextString()));
            lexer = new Scanner("Or", ParserOptions.AllowAll);
            Assert.True(lexer.Advance());
            Assert.True(lexer.IsNext(Token.TokenType.Or));
            Assert.Equal(0, String.Compare(Token.Or.String, lexer.IsNextString()));
        }
예제 #7
0
        private string DecodeCharacters(Scanner scanner)
        {
            var  sb = new StringBuilder();
            byte peek;

            while (!scanner.ReachedEnd && (peek = scanner.PeekByte()) != StringSegmentCodes.StringTerminator)
            {
                if (peek >= 0x80)
                {
                    NextCharacter(scanner, sb);
                }
                else if (peek == StringSegmentCodes.LineBreak)
                {
                    scanner.Advance();
                    sb.Append("\n");
                }
                else
                {
                    break;
                }
            }

            return(sb.ToString());
        }
예제 #8
0
        private SC3StringSegment NextSegment(Scanner scanner)
        {
            byte peek = scanner.PeekByte();

            switch (peek)
            {
            case StringSegmentCodes.LineBreak:
                scanner.Advance();
                return(new Marker(MarkerKind.LineSeperator));

            case StringSegmentCodes.CharacterName:
                scanner.Advance();
                return(new Marker(MarkerKind.CharacterName));

            case StringSegmentCodes.DialogueLine:
                scanner.Advance();
                return(new Marker(MarkerKind.DialogueLine));

            case StringSegmentCodes.RubyBase:
                scanner.Advance();
                return(new Marker(MarkerKind.RubyBase));

            case StringSegmentCodes.RubyTextStart:
                scanner.Advance();
                return(new Marker(MarkerKind.RubyTextStart));

            case StringSegmentCodes.RubyTextEnd:
                scanner.Advance();
                return(new Marker(MarkerKind.RubyTextEnd));

            case StringSegmentCodes.NameAndCode:
                scanner.Advance();
                return(new Marker(MarkerKind.NameAndCode));

            case StringSegmentCodes.TempMarker:
                scanner.Advance();
                return(new Marker(MarkerKind.TempMarker));

            case StringSegmentCodes.SetColor:
                scanner.Advance();
                var colorIndex = SC3ExpressionParser.ParseExpression(scanner.Reader);
                return(new SetColorCommand(colorIndex));

            case StringSegmentCodes.SetFontSize:
                scanner.Advance();
                int fontSize = scanner.Reader.ReadInt16();
                return(new SetFontSizeCommand(fontSize));

            case StringSegmentCodes.SetAlignment_Center:
                scanner.Advance();
                return(new CenterTextCommand());

            case StringSegmentCodes.SetTopMargin:
                scanner.Advance();
                int topMargin = scanner.Reader.ReadInt16();
                return(new SetMarginCommand(null, topMargin));

            case StringSegmentCodes.SetLeftMargin:
                scanner.Advance();
                int leftMargin = scanner.Reader.ReadInt16();
                return(new SetMarginCommand(leftMargin, null));

            case StringSegmentCodes.EvaluateExpression:
                scanner.Advance();
                var expression = SC3ExpressionParser.ParseExpression(scanner.Reader);
                return(new EvaluateExpressionCommand(expression));

            case StringSegmentCodes.Present:
                scanner.Advance();
                return(new PresentCommand(resetTextAlignment: false));

            case StringSegmentCodes.Present_ResetAlignment:
                scanner.Advance();
                return(new PresentCommand(resetTextAlignment: true));

            default:
                peek = scanner.PeekByte();
                if (IsCharacter(peek))
                {
                    string value = DecodeCharacters(scanner);
                    return(new TextSegment(value));
                }
                else
                {
                    throw ExceptionUtils.SC3String_UnexpectedByte(peek);
                }
            }
        }
예제 #9
0
        public void NegativeTests()
        {
            Scanner lexer = new Scanner("'$(DEBUG) == true", ParserOptions.AllowAll);

            Assert.False(lexer.Advance());
        }
예제 #10
0
        public void WhitespaceTests()
        {
            Scanner lexer;

            Console.WriteLine("here");
            lexer = new Scanner("$(DEBUG) and $(FOO)", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.And));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));

            lexer = new Scanner("1234$(DEBUG)0xabcd@(foo)asdf<>'foo'<=false>=true==1234!=", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LessThan));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.GreaterThan));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LessThanOrEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.GreaterThanOrEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.EqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.NotEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.EndOfInput));

            lexer = new Scanner("   1234    $(DEBUG)    0xabcd  \n@(foo)    \nasdf  \n<     \n>     \n'foo'  \n<=    \nfalse     \n>=    \ntrue  \n== \n 1234    \n!=     ", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LessThan));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.GreaterThan));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LessThanOrEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.GreaterThanOrEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.EqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.NotEqualTo));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.EndOfInput));
        }
예제 #11
0
        public void FunctionTests()
        {
            Scanner lexer = new Scanner("Foo()", ParserOptions.AllowAll);

            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( 1 )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( $(Property) )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( @(ItemList) )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( simplestring )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( 'Not a Simple String' )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( 'Not a Simple String', 1234 )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( $(Property), 'Not a Simple String', 1234 )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));

            lexer = new Scanner("Foo( @(ItemList), $(Property), simplestring, 'Not a Simple String', 1234 )", ParserOptions.AllowAll);
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Function));
            Assert.Equal(0, String.Compare("Foo", lexer.IsNextString()));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.LeftParenthesis));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.ItemList));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Property));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.String));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Comma));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.Numeric));
            Assert.True(lexer.Advance() && lexer.IsNext(Token.TokenType.RightParenthesis));
        }