Coverage for src/botadi/mokadi/mokadi_grammar.py: 75%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Helpers with grammar for mokadi.
4"""
5from .mokadi_parser import get_tree_string, parse_mokadi, run_parse
6from .mokadi_exceptions import MokadiException
7from .grammars import MokadiGrammar_frParser, MokadiGrammar_frLexer, MokadiGrammar_frListener
10def interpret(sentance, MokadiGrammarParser=None, MokadiGrammarLexer=None,
11 MokadiGrammarListener=None):
12 """
13 Interprets a sentance and returns a list of words.
15 @param MokadiGrammarParser parser for a specific language
16 @param MokadiGrammarLexer lexer for a specific language
17 @param MokadiGrammarListener listener for a specific language
18 @param sentance any string
19 @return list of tuple (word, kind)
20 """
21 if MokadiGrammarParser is None:
22 MokadiGrammarParser = MokadiGrammar_frParser
23 if MokadiGrammarLexer is None:
24 MokadiGrammarLexer = MokadiGrammar_frLexer
25 if MokadiGrammarListener is None:
26 MokadiGrammarListener = MokadiGrammar_frListener
27 parser = parse_mokadi(sentance, MokadiGrammarParser, MokadiGrammarLexer)
28 stdout, stderr, tree = run_parse(parser)
29 if stderr and len(stderr) > 0:
30 raise MokadiException(
31 "Unable to parse '{0}'\nOUT\n{1}\nERR\n{2}".format(sentance, stdout, stderr))
32 _, simple = get_tree_string(
33 MokadiGrammarListener, tree, parser, sentance)
34 return simple