Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Prio,
Choice,
THIS,
Ref,
List,
Grammar,
KeywordError,
ReKeywordsChangedError,
NameAssignedError,
MissingStartError,
UnusedElementError) # nopep8
from examples import JsonGrammar # nopep8
class _TestGrammar1(Grammar):
k_test = Keyword('test')
k_ignore_case = Keyword('ignore_case', ign_case=True)
START = Sequence(k_test, k_ignore_case)
class _TestGrammar2(Grammar):
k_ni = Keyword('ni')
s_seq = Sequence('(', THIS, ')')
START = Prio(
k_ni,
s_seq,
Sequence(THIS, Keyword('and'), THIS),
Sequence(THIS, Keyword('or'), THIS))
class _TestGrammar3(Grammar):
s_tic_tac = Sequence(Keyword('tic'), Keyword('tac'))
START = Sequence(k_test, k_ignore_case)
class _TestGrammar2(Grammar):
k_ni = Keyword('ni')
s_seq = Sequence('(', THIS, ')')
START = Prio(
k_ni,
s_seq,
Sequence(THIS, Keyword('and'), THIS),
Sequence(THIS, Keyword('or'), THIS))
class _TestGrammar3(Grammar):
s_tic_tac = Sequence(Keyword('tic'), Keyword('tac'))
s_tic_tac_toe = Sequence(Keyword('tic'), Keyword('tac'), Keyword('toe'))
START = Sequence(
Choice(s_tic_tac, s_tic_tac_toe),
Choice(s_tic_tac, s_tic_tac_toe, most_greedy=False))
class _TestGrammar4(Grammar):
START = Ref()
ni_item = Choice(Keyword('ni'), START)
START = Sequence('[', List(ni_item), ']')
class TestPyleri(unittest.TestCase):
def setUp(self):
gc.collect()
def test_compile_error(self):
with self.assertRaises(ReKeywordsChangedError):
class _G1(Grammar):
k_test = Keyword('test')
RE_KEYWORDS = '[a-z]+'
with self.assertRaises(KeywordError):
class _G2(Grammar):
k_test = Keyword('test-nomatch')
with self.assertRaises(KeywordError):
# test 'deep' keyword checks.
class _G3(Grammar):
START = Optional(Keyword('test-nomatch'))
with self.assertRaises(NameAssignedError):
class _G4(Grammar):
k_test = Keyword('test')
k_test2 = k_test
with self.assertRaises(NameAssignedError):
class _G5(Grammar):
k_test = Keyword('test')
k_Test = Keyword('duplicate')
with self.assertRaises(MissingStartError):
class _G6(Grammar):
k_test = Keyword('test')
with self.assertRaises(UnusedElementError):
import json
from pyleri import Choice
from pyleri import Grammar
from pyleri import Keyword
from pyleri import Regex
from pyleri import Repeat
from pyleri import Sequence
# Create a Grammar Class to define your language
class MyGrammar(Grammar):
r_name = Regex('(?:"(?:[^"]*)")+')
k_hi = Keyword('hi')
k_bye = Keyword('bye')
START = Repeat(Sequence(Choice(k_hi, k_bye), r_name))
# Returns properties of a node object as a dictionary:
def node_props(node, children):
return {
'start': node.start,
'end': node.end,
'name': node.element.name if hasattr(node.element, 'name') else None,
'element': node.element.__class__.__name__,
'string': node.string,
'children': children}
def test_compile_error(self):
with self.assertRaises(ReKeywordsChangedError):
class _G1(Grammar):
k_test = Keyword('test')
RE_KEYWORDS = '[a-z]+'
with self.assertRaises(KeywordError):
class _G2(Grammar):
k_test = Keyword('test-nomatch')
with self.assertRaises(KeywordError):
# test 'deep' keyword checks.
class _G3(Grammar):
START = Optional(Keyword('test-nomatch'))
with self.assertRaises(NameAssignedError):
class _G4(Grammar):
k_test = Keyword('test')
k_test2 = k_test
with self.assertRaises(NameAssignedError):
class _G5(Grammar):
k_test = Keyword('test')
k_Test = Keyword('duplicate')
with self.assertRaises(MissingStartError):
class _G6(Grammar):
k_test = Keyword('test')
def test_compile_error(self):
with self.assertRaises(ReKeywordsChangedError):
class _G1(Grammar):
k_test = Keyword('test')
RE_KEYWORDS = '[a-z]+'
with self.assertRaises(KeywordError):
class _G2(Grammar):
k_test = Keyword('test-nomatch')
with self.assertRaises(KeywordError):
# test 'deep' keyword checks.
class _G3(Grammar):
START = Optional(Keyword('test-nomatch'))
with self.assertRaises(NameAssignedError):
class _G4(Grammar):
k_test = Keyword('test')
k_test2 = k_test
import json
from pyleri import Choice
from pyleri import Grammar
from pyleri import Keyword
from pyleri import Regex
from pyleri import Repeat
from pyleri import Sequence
# Create a Grammar Class to define your language
class MyGrammar(Grammar):
r_name = Regex('(?:"(?:[^"]*)")+')
k_hi = Keyword('hi')
k_bye = Keyword('bye')
START = Repeat(Sequence(Choice(k_hi, k_bye), r_name))
# Returns properties of a node object as a dictionary:
def node_props(node, children):
return {
'start': node.start,
'end': node.end,
'name': node.element.name if hasattr(node.element, 'name') else None,
'element': node.element.__class__.__name__,
'string': node.string,
'children': children}
k_test = Keyword('test')
k_ignore_case = Keyword('ignore_case', ign_case=True)
START = Sequence(k_test, k_ignore_case)
class _TestGrammar2(Grammar):
k_ni = Keyword('ni')
s_seq = Sequence('(', THIS, ')')
START = Prio(
k_ni,
s_seq,
Sequence(THIS, Keyword('and'), THIS),
Sequence(THIS, Keyword('or'), THIS))
class _TestGrammar3(Grammar):
s_tic_tac = Sequence(Keyword('tic'), Keyword('tac'))
s_tic_tac_toe = Sequence(Keyword('tic'), Keyword('tac'), Keyword('toe'))
START = Sequence(
Choice(s_tic_tac, s_tic_tac_toe),
Choice(s_tic_tac, s_tic_tac_toe, most_greedy=False))
class _TestGrammar4(Grammar):
START = Ref()
ni_item = Choice(Keyword('ni'), START)
START = Sequence('[', List(ni_item), ']')
class TestPyleri(unittest.TestCase):
import re
import random
from pyleri import Choice
from pyleri import Grammar
from pyleri import Keyword
from pyleri import Repeat
from pyleri import Sequence
from pyleri import end_of_statement
# Create a Grammar Class to define your language.
class MyGrammar(Grammar):
RE_KEYWORDS = re.compile('\S+')
r_name = Keyword('"pyleri"')
k_hi = Keyword('hi')
k_bye = Keyword('bye')
START = Repeat(Sequence(Choice(k_hi, k_bye), r_name), mi=2)
# Print the expected elements as a indented and numbered list.
def print_expecting(node_expecting, string_expecting):
for loop, e in enumerate(node_expecting):
string_expecting = '{}\n\t({}) {}'.format(string_expecting, loop, e)
print(string_expecting)
# Complete a string until it is valid according to the grammar.
def auto_correction(string, my_grammar):
Sequence(THIS, Keyword('or'), THIS))
class _TestGrammar3(Grammar):
s_tic_tac = Sequence(Keyword('tic'), Keyword('tac'))
s_tic_tac_toe = Sequence(Keyword('tic'), Keyword('tac'), Keyword('toe'))
START = Sequence(
Choice(s_tic_tac, s_tic_tac_toe),
Choice(s_tic_tac, s_tic_tac_toe, most_greedy=False))
class _TestGrammar4(Grammar):
START = Ref()
ni_item = Choice(Keyword('ni'), START)
START = Sequence('[', List(ni_item), ']')
class TestPyleri(unittest.TestCase):
def setUp(self):
gc.collect()
def test_parse_keyword(self):
tg = _TestGrammar1()
self.assertTrue(tg.parse('test ignore_case').is_valid)
self.assertTrue(tg.parse('test Ignore_Case').is_valid)
# test is not case insensitive
self.assertFalse(tg.parse('Test ignore_case').is_valid)
# - is not _
self.assertFalse(tg.parse('test ignore-case').is_valid)