Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
scanner.py
Go to the documentation of this file.
1 ''' @file apg_py/api/scanner.py
2 @brief Scans the SABNF source for invalid characters.'''
3 from apg_py.api.scanner_callbacks import scanner_line
4 from apg_py.api.scanner_callbacks import scanner_last_line
5 from apg_py.api.scanner_callbacks import scanner_line_text
6 from apg_py.api.scanner_callbacks import scanner_invalid
7 from apg_py.api.scanner_callbacks import scanner_end
8 from apg_py.api.scanner_callbacks import scanner_lf
9 from apg_py.api.scanner_callbacks import scanner_cr
10 from apg_py.api.scanner_callbacks import scanner_crlf
11 from apg_py.api import scanner_grammar
12 from apg_py.lib.parser import Parser
13 from apg_py.lib.ast import Ast
14 
15 
16 def scanner(input, strict):
17  '''Scan the input for invalid characters and construct a line catalog
18  for looking up line numbers from character indexes.
19  @param input A tuple of positive integers. Often, but not necessarily,
20  character codes.
21  @param strict If True, only RFC 5234 line endings allowed (CRLF).
22  @returns Returns a dictionary of errors, if any, and the line catalog.
23  {'errors': data['errors'], 'lines': data['lines']}
24  '''
25  parser = Parser(scanner_grammar)
26  ast = Ast(parser)
27  ast.add_callback('line', scanner_line)
28  ast.add_callback('last-line', scanner_last_line)
29  ast.add_callback('line-text', scanner_line_text)
30  ast.add_callback('invalid', scanner_invalid)
31  ast.add_callback('end', scanner_end)
32  ast.add_callback('lf', scanner_lf)
33  ast.add_callback('cr', scanner_cr)
34  ast.add_callback('crlf', scanner_crlf)
35  result = parser.parse(input)
36  if(result.success is False):
37  # this should never happen
38  msg = 'Unexpected error scanning input grammar.' \
39  + ' Grammar is seriously malformed.'
40  raise Exception(msg)
41  data = {}
42  data['strict'] = strict
43  data['lines'] = []
44  data['line_no'] = 0
45  data['line_end'] = None
46  data['errors'] = []
47  ast.translate(data)
48  return {'errors': data['errors'], 'lines': data['lines']}
A class for capturing the AST as the parser traverses the parse tree.
Definition: ast.py:69
The Parser class for parsing an APG grammar.
Definition: parser.py:60
def scanner(input, strict)
Scan the input for invalid characters and construct a line catalog for looking up line numbers from c...
Definition: scanner.py:16
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.