Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
syntax_callbacks.py
Go to the documentation of this file.
1 ''' @file apg_py/api/syntax_callbacks.py
2 @brief All the syntax parser's callback functions.
3 '''
4 from apg_py.lib import identifiers as id
5 
6 
7 def add_syntax_callbacks(parser):
8  parser.add_callbacks({'rule': syntax_rule_error})
9  parser.add_callbacks({'rule-name': syntax_rule_name_error})
10  parser.add_callbacks({'equals': syntax_defined_as_error})
11  parser.add_callbacks({'group-close': syntax_group_close_error})
12  parser.add_callbacks({'option-close': syntax_option_close_error})
13  parser.add_callbacks({'tls-close': syntax_tls_close_error})
14  parser.add_callbacks({'cls-close': syntax_cls_close_error})
15  parser.add_callbacks({'string-tab': syntax_string_tab})
16  parser.add_callbacks({'pros-val': syntax_pros_val})
17  parser.add_callbacks({'pros-val-close': syntax_pros_val_close_error})
18  parser.add_callbacks({'udt-op': syntax_udt_op})
19  parser.add_callbacks({'and-op': syntax_and_op})
20  parser.add_callbacks({'not-op': syntax_not_op})
21  parser.add_callbacks({'bkr-op': syntax_bkr_op})
22  parser.add_callbacks({'bka-op': syntax_bka_op})
23  parser.add_callbacks({'bkn-op': syntax_bkn_op})
24  parser.add_callbacks({'abg-op': syntax_abg_op})
25  parser.add_callbacks({'aen-op': syntax_aen_op})
26 
27 
28 def append_error(user_data, index, msg):
29  user_data['errors'].append({
30  'line': user_data['find_line'](index),
31  'index': index,
32  'msg': msg
33  })
34 
35 
37  if(data['state'] is id.NOMATCH):
38  msg = 'malformed rule (^ indicates approximate error location)'
39  append_error(data['user_data'], data['max_phrase_length'], msg)
40 
41 
43  if(data['state'] is id.NOMATCH):
44  msg = 'malformed rule name'
45  append_error(data['user_data'], data['phrase_index'], msg)
46 
47 
49  if(data['state'] is id.NOMATCH):
50  msg = 'expected "defined as"(=/ or =) not found'
51  append_error(data['user_data'], data['phrase_index'], msg)
52 
53 
55  if(data['state'] is id.NOMATCH):
56  msg = 'expected group closure, ")", not found'
57  append_error(data['user_data'], data['phrase_index'], msg)
58 
59 
61  if(data['state'] is id.NOMATCH):
62  msg = 'expected option closure, "]", not found'
63  append_error(data['user_data'], data['phrase_index'], msg)
64 
65 
67  if(data['state'] == id.NOMATCH):
68  msg = 'expected literal string closure, ", not found'
69  append_error(data['user_data'], data['phrase_index'], msg)
70 
71 
73  if(data['state'] == id.NOMATCH):
74  msg = "expected case-sensitive literal string closure, ', not found"
75  append_error(data['user_data'], data['phrase_index'], msg)
76 
77 
79  if(data['state'] == id.MATCH):
80  msg = "tab characters not allowed in quoted strings or prose values"
81  append_error(data['user_data'], data['phrase_index'], msg)
82 
83 
84 def syntax_pros_val(data):
85  if(data['state'] == id.MATCH):
86  msg = "prose values are valid ABNF syntax but "
87  msg += "but no parser operator can be generated"
88  append_error(data['user_data'], data['phrase_index'], msg)
89 
90 
92  if(data['state'] == id.NOMATCH):
93  msg = "expected prose value closure, >, not found"
94  append_error(data['user_data'], data['phrase_index'], msg)
95 
96 
97 def syntax_udt_op(data):
98  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
99  msg = "UDTs (User Defined Terminals, u_name & e_name) not allowed "
100  msg += "with strict ABNF"
101  append_error(data['user_data'], data['phrase_index'], msg)
102 
103 
104 def syntax_and_op(data):
105  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
106  msg = "& operator (postive look ahead) not allowed "
107  msg += "with strict ABNF"
108  append_error(data['user_data'], data['phrase_index'], msg)
109 
110 
111 def syntax_not_op(data):
112  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
113  msg = "! operator (negative look ahead) not allowed "
114  msg += "with strict ABNF"
115  append_error(data['user_data'], data['phrase_index'], msg)
116 
117 
118 def syntax_bkr_op(data):
119  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
120  msg = "\\ operator (back referencing) not allowed "
121  msg += "with strict ABNF"
122  append_error(data['user_data'], data['phrase_index'], msg)
123 
124 
125 def syntax_bka_op(data):
126  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
127  msg = "&& operator (positive look behind) not allowed "
128  msg += "with strict ABNF"
129  append_error(data['user_data'], data['phrase_index'], msg)
130 
131 
132 def syntax_bkn_op(data):
133  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
134  msg = "!! operator (negative look behind) not allowed "
135  msg += "with strict ABNF"
136  append_error(data['user_data'], data['phrase_index'], msg)
137 
138 
139 def syntax_abg_op(data):
140  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
141  msg = "%^ operator (begin of input anchor) not allowed "
142  msg += "with strict ABNF"
143  append_error(data['user_data'], data['phrase_index'], msg)
144 
145 
146 def syntax_aen_op(data):
147  if(data['state'] is id.MATCH and data['user_data']['strict'] is True):
148  msg = "%$ operator (end of input anchor) not allowed "
149  msg += "with strict ABNF"
150  append_error(data['user_data'], data['phrase_index'], msg)
def append_error(user_data, index, msg)
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.