Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
syntax.py
Go to the documentation of this file.
1 ''' @file apg_py/api/syntax.py
2 @brief Parse the SABNF grammar for syntax errors.
3 
4 Generate the AST for the semantic phase (@ref semantic.py)
5 '''
6 # for development debugging only
7 from apg_py.lib.trace import Trace
8 
9 
10 def syntax(api, strict):
11  '''Parse the SABNF grammar.
12  @param api The api object (@ref api.py) for the given grammar syntax.
13  @param strict If True, strictly follow the ABNF conventions of RFCs 5234 & 7405
14  @returns Returns a list of error, if any. Each error in the list has
15  the dictionary form
16  - 'line': the line number where the error occurred
17  - 'index': the approximate relative index within the line
18  where the error occurred
19  - 'msg': as descriptive error message
20  '''
21  data = {}
22  data['find_line'] = api.find_line
23  data['strict'] = strict
24  data['errors'] = []
25  data['max_index'] = 0
26  # Trace(api.parser, mode='xc')
27  result = api.parser.parse(api.input, user_data=data)
28  if(result.success is False):
29  # raise Exception('Internal Error: syntax parser should never fail')
30  msg = 'fatal syntax error encountered'
31  msg += ' - parser stopped approximately here'
32  data['errors'].append({
33  'line': api.find_line(result.max_phrase_length),
34  'index': result.max_phrase_length,
35  'msg': msg
36  })
37  return data['errors']
def syntax(api, strict)
Parse the SABNF grammar.
Definition: syntax.py:10
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.