1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_parser_params
# Purpose: Test for parser parameters.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
from __future__ import unicode_literals
import pytest
from arpeggio import ParserPython, NoMatch
from arpeggio import RegExMatch as _
def test_autokwd():
"""
autokwd will match keywords on word boundaries.
"""
def grammar(): return ("one", "two", "three")
parser = ParserPython(grammar, autokwd=True)
# If autokwd is enabled this should parse without error.
parser.parse("one two three")
# But this will not parse because each word to match
# will be, by default, tried to match as a whole word
with pytest.raises(NoMatch):
parser.parse("onetwothree")
parser = ParserPython(grammar, autokwd=False)
# If we turn off the autokwd than this will match.
parser.parse("one two three")
parser.parse("onetwothree")
def test_skipws():
"""
skipws will skip whitespaces.
"""
def grammar(): return ("one", "two", "three")
parser = ParserPython(grammar)
# If skipws is on this should parse without error.
parser.parse("one two three")
# If not the same input will raise exception.
parser = ParserPython(grammar, skipws=False)
with pytest.raises(NoMatch):
parser.parse("one two three")
def test_ws():
"""
ws consists of chars that will be skipped if skipws is enables.
By default it consists of space, tab and newline.
"""
def grammar(): return ("one", "two", "three")
parser = ParserPython(grammar)
# With default ws this should parse without error
parser.parse("""one
two three""")
# If we make only a space char to be ws than the
# same input will raise exception.
parser = ParserPython(grammar, ws=" ")
with pytest.raises(NoMatch):
parser.parse("""one
two three""")
# But if only spaces are between words than it will
# parse.
parser.parse("one two three")