Exemplo n.º 1
0
        public void single_item_without_terminator_fails()
        {
            var scanner = new ScanStrings("one");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.False, "Result success");
        }
Exemplo n.º 2
0
        public void delimiter_only_fails()
        {
            var scanner = new ScanStrings(";");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.False, "Result success");
        }
Exemplo n.º 3
0
        private void BTN_Parse_Click(object sender, EventArgs e)
        {
            feedBox.Clear();
            string input = inputBox.Text;

            Parser eof = new Phantom.Parsers.Terminals.EndOfInput();

            ScanStrings scanner = new ScanStrings(input);
            scanner.SkipWhitespace = true;
            scanner.Transform = new TransformToLower();
            XMLParser pp = new XMLParser(); //new PascalParser();
            Parser r1 = pp.TheParser > eof;

            //Parser r1 = (Parser)"hello" > "," > "world" > "!" > eof; // dead simple, matches "Hello, world!";

            r1.ScannerInput = scanner;

            ParserMatch m = r1.Compare();

            if (m.Success) {
                resultsBox.Text = m.ToString();
                // grab all bottom matches:
                List<ParserMatch> matches = new List<ParserMatch>(m.BottomLevelMatches());

                foreach (ParserMatch chunk in matches) {
                    feedBox.Text += chunk.Value + "\r\n";
                }
            } else {
                resultsBox.Text = "*NO MATCH*\r\n" + scanner.BadPatch(5);
            }

            /*foreach (string s in pp.ResultList) {
                feedBox.Text += s + "\r\n";
            }*/
        }
Exemplo n.º 4
0
        public void multiple_items_with_end_terminator_missing_passes_finding_only_terminated_items()
        {
            var scanner = new ScanStrings("one;two");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.Value, Is.EqualTo("one;"));
        }
Exemplo n.º 5
0
        public void regex_dot_matches_carriage_return_but_not_line_feed()
        {
            IMatchingParser subject = new RegularExpression(@".*");
            var scanner = new ScanStrings("line one\r\nline two");
            var result = subject.TryMatch(scanner);

            Assert.That(result.Value, Contains.Substring("\r"));
        }
Exemplo n.º 6
0
 public void a_scanner_and_some_terminals()
 {
     scanner = new ScanStrings(Input) { SkipWhitespace = true };
     __this__ = new LiteralString("this");
     __is__ = new LiteralString("is");
     __wally__ = new LiteralString("wally");
     __dr_jones__ = new LiteralString("Dr. Jones!");
 }
Exemplo n.º 7
0
        public void single_item_without_delimiter_passes()
        {
            var scanner = new ScanStrings("one");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.Value, Is.EqualTo("one"));
        }
Exemplo n.º 8
0
        public void multiple_items_with_end_delimiter_passes_but_does_not_match_end_delimiter()
        {
            var scanner = new ScanStrings("one;two;");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.Value, Is.EqualTo("one;two"));
        }
Exemplo n.º 9
0
        public void multiple_items_with_separated_by_delimiter_are_all_found()
        {
            var scanner = new ScanStrings("one;two");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.Value, Is.EqualTo("one;two"));
        }
Exemplo n.º 10
0
        public void double_delimiter_matches_single_item_only_without_delimited()
        {
            var scanner = new ScanStrings("one;;");
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.Value, Is.EqualTo("one"));
        }
Exemplo n.º 11
0
        public void regexes_are_single_line_setting_by_default(string regex, string inputString, bool isMatch)
        {
            IMatchingParser subject = new RegularExpression(regex);
            var scanner = new ScanStrings(inputString);
            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.EqualTo(isMatch), "Match result");
        }
Exemplo n.º 12
0
        public void child_matches_are_the_separated_items()
        {
            var scanner = new ScanStrings("one;two;three;");
            var expectedItems = new[] { "one", ";", "two", ";", "three" };
            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True, "Result success");
            Assert.That(result.ChildMatches.Select(p=>p.Value), Is.EquivalentTo(expectedItems));
        }
Exemplo n.º 13
0
        public void BNF_terminated_list_matches_input_correctly()
        {
            var input = new ScanStrings("hello hello jello hello"){SkipWhitespace = true};
            var subject = +((BNF)"hello");
            var result = subject.Result().Parse(input);

            Assert.That(result.Success, Is.True);
            Assert.That(result.Value, Is.EqualTo("hello hello"));
        }
        public void regular_expression_parser_can_have_Regex_options_set(string inputString, string pattern, RegexOptions options, bool success, string match)
        {
            var subject = new RegularExpression(pattern, options);
            var scanner = new ScanStrings(inputString);
            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.EqualTo(success));
            if (success) Assert.That(result.Value, Is.EqualTo(match));
        }
Exemplo n.º 15
0
        public void non_matching_regexes_result_in_failing_parses_and_do_not_advance_the_scanner(string regex, string inputString)
        {
            IMatchingParser subject = new RegularExpression(regex);
            var scanner = new ScanStrings(inputString);
            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.False, "Match result");
            Assert.That(scanner.Offset, Is.EqualTo(0), "Scanner offset");
        }
Exemplo n.º 16
0
        public void bnf_options_concatenate_into_large_options(string source, bool passes)
        {
            var scanner = new ScanStrings(source){SkipWhitespace = true};
            var subject = (BNF)"hello world" | "mind the gap" | "once in a lifetime" | "modern greece";

            var result = subject.Result().Parse(scanner);

            Assert.That(result.Success, Is.EqualTo(passes));
        }
        public void Lowercases_normal_characters(string input, string expected)
        {
            var scanner = new ScanStrings(input);
            scanner.Transform = new TransformToLower();

            var result = scanner.Substring(0, input.Length);

            Assert.That(result, Is.EqualTo(expected));
        }
Exemplo n.º 18
0
 public void creating_with_a_string_and_offset_starts_at_offset_and_scans_to_end()
 {
     IScanner subject = new ScanStrings("input", 2);
     Assert.That(subject.EndOfInput, Is.False);
     Assert.That(subject.Peek(), Is.EqualTo('p')); Assert.That(subject.Read(), Is.True);
     Assert.That(subject.Peek(), Is.EqualTo('u')); Assert.That(subject.Read(), Is.True);
     Assert.That(subject.Peek(), Is.EqualTo('t')); Assert.That(subject.Read(), Is.False);
     Assert.That(subject.EndOfInput, Is.True);
 }
Exemplo n.º 19
0
        public void bnf_intersections_match_input_correctly(string source, bool passes)
        {
            var scanner = new ScanStrings(source){SkipWhitespace = true};
            var subject = (BNF)"world" & "hello";

            var result = subject.Result().Parse(scanner);

            Assert.That(result.Success, Is.EqualTo(passes), string.Join(", ",scanner.ListFailures()));
        }
Exemplo n.º 20
0
        public void BNF_sequence_matches_input_correctly()
        {
            var input = new ScanStrings("hello, world"){SkipWhitespace = true};
            var subject = (BNF)"hello" > ',' > "world";
            var result = subject.Result().Parse(input);

            Assert.That(result.Success, Is.True);
            Assert.That(result.Value, Is.EqualTo("hello, world"));
        }
Exemplo n.º 21
0
        public void regexes_whose_match_does_not_start_at_the_current_scanner_position_are_failures()
        {
            IMatchingParser subject = new RegularExpression(@"\w+");
            var scanner = new ScanStrings("      some words");

            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.False);
        }
Exemplo n.º 22
0
        public void matching_regexes_result_in_matching_parses_and_contain_the_match_and_advance_the_scanner(string regex, string inputString, string match)
        {
            IMatchingParser subject = new RegularExpression(regex);
            var scanner = new ScanStrings(inputString);
            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.True, "Match result");
            Assert.That(result.Value, Is.EqualTo(match), "Parser match string");
            Assert.That(scanner.Offset, Is.EqualTo(match.Length), "Scanner offset");
        }
Exemplo n.º 23
0
        public void InvalidProgramFails(string program)
        {
            var parser = new PascalParser().TheParser;
            var scanner = new ScanStrings(missing_quote){SkipWhitespace = true};
            scanner.Transform = new TransformToLower();

            var result = parser.Parse(scanner);

            Assert.That(result.Success, Is.False);
        }
Exemplo n.º 24
0
        public void XmlDocumentParsesSuccessfully()
        {
            var parser = new XMLParser().TheParser;
            var scanner = new ScanStrings(sample);

            var result = parser.Parse(scanner);

            Assert.That(result.Success, Is.True, result + ": " + result.Value);
            Assert.That(result.Value, Is.EqualTo(sample));
        }
Exemplo n.º 25
0
        public void regexes_that_can_match_empty_strings_can_return_successful_empty_matches()
        {
            IMatchingParser subject = new RegularExpression(@"\W*");
            var scanner = new ScanStrings("123456");

            var result = subject.TryMatch(scanner);

            Assert.That(result.Success, Is.True);
            Assert.That(result.Value, Is.EqualTo(""));
        }
Exemplo n.º 26
0
        public void input_that_repeats_less_than_parsers_lower_bound_fails()
        {
            var term = new LiteralString("op!");
            var scanner = new ScanStrings("op!op!");
            IParser subject = new Repetition(term, 4, 5);

            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.False);
        }
Exemplo n.º 27
0
        public void input_that_repeats_more_than_parsers_upper_bound_passes_and_captures_only_expected_max_range()
        {
            var term = new LiteralString("op!");
            var scanner = new ScanStrings("op!op!op!op!op!op!op!op!");
            IParser subject = new Repetition(term, 3, 3);

            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True);
            Assert.That(result.Value, Is.EqualTo("op!op!op!"));
        }
Exemplo n.º 28
0
        public void BasicPascalProgramParsesOK()
        {
            var parser = new PascalParser().TheParser;
            var scanner = new ScanStrings(sample_program){SkipWhitespace = true};
            scanner.Transform = new TransformToLower();

            var result = parser.Parse(scanner);

            Assert.That(result.Success, Is.True, String.Join("\n\n", scanner.ListFailures()));
            Assert.That(result.Value.ToLower(), Is.EqualTo(sample_program.ToLower()));
        }
Exemplo n.º 29
0
        public void input_that_repeats_exactly_as_many_as_the_upper_bound_passes_and_captures_all_input()
        {
            var term = new LiteralString("op!");
            var scanner = new ScanStrings("op!op!op!");
            IParser subject = new Repetition(term, 1, 3);

            var result = subject.Parse(scanner);

            Assert.That(result.Success, Is.True);
            Assert.That(result.Value, Is.EqualTo("op!op!op!"));
        }
Exemplo n.º 30
0
 public void a_string_scanner_with_some_text()
 {
     // Newline is char 3+
     oneLineScanner = new ScanStrings(oneLineInput);
     unix = new ScanStrings("-> \n <-");
     windows = new ScanStrings("-> \r\n <-");
     oldMac = new ScanStrings("-> \r <-");
     twoUnix = new ScanStrings("-> \n\n <-");
     twoWindows = new ScanStrings("-> \r\n\r\n <-");
     mangledTwo = new ScanStrings("-> \n\r <-");
     subject = new EndOfLine();
 }