Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
ast_callbacks.py
Go to the documentation of this file.
1 ''' @file examples/ini_file/ast_callbacks.py
2 @brief The AST call back functions for the ini file class.
3 '''
4 import sys
5 import os
6 # add the current working directory to the path
7 # DO NOT MOVE THE FOLLOWING STATEMENT
8 # if using autopep8 formatter, for example, set argument '--ignore=E402'
9 sys.path.append(os.getcwd())
10 from apg_py.lib import identifiers as id
11 from apg_py.lib import utilities as utils
12 
13 
14 def section_name(state, input, index, length, data):
15  if(state == id.SEM_PRE):
16  name = utils.tuple_to_string(input[index:index + length])
17  if(name != data['current_section']):
18  data['current_section'] = name
19  if(not data['sections'].get(name)):
20  data['sections'][name] = {}
21 
22 
23 def key_name(state, input, index, length, data):
24  if(state == id.SEM_PRE):
25  name = utils.tuple_to_string(input[index:index + length])
26  if(data['current_section']):
27  section = data['sections'][data['current_section']]
28  else:
29  section = data['global']
30  key = section.get(name)
31  if(not key):
32  section[name] = []
33  data['current_key'] = name
34 
35 
36 def value(state, input, index, length, data):
37  if(state == id.SEM_POST):
38  if(data['current_section']):
39  section = data['sections'][data['current_section']]
40  else:
41  section = data['global']
42  section[data['current_key']].append(data['value'])
43 
44 
45 def hex_digit(d):
46  if(d >= 48 and d <= 57):
47  return d - 48
48  if(d >= 65 and d <= 72):
49  return d - 55
50  if(d >= 97 and d <= 104):
51  return d - 87
52  raise Exception('bad hex digit', d)
53 
54 
55 def string_eval(input):
56  value = ''
57  skip = 0
58  for i in range(len(input)):
59  if(skip):
60  skip -= 1
61  else:
62  if(input[i] == 92):
63  if(input[i + 1] == 120):
64  c = hex_digit(input[i + 2])
65  c = 16 * c + hex_digit(input[i + 3])
66  value += chr(c)
67  skip = 3
68  elif(input[i + 1] == 117):
69  c = hex_digit(input[i + 2])
70  c = 16 * c + hex_digit(input[i + 3])
71  c = 16 * c + hex_digit(input[i + 4])
72  c = 16 * c + hex_digit(input[i + 5])
73  value += chr(c)
74  skip = 5
75  else:
76  if(input[i + 1] == 116):
77  value += chr(0x09)
78  elif(input[i + 1] == 114):
79  value += chr(0x0D)
80  elif(input[i + 1] == 110):
81  value += chr(0x0A)
82  elif(input[i + 1] == 98):
83  value += chr(0x20)
84  else:
85  value += chr(input[i + 1])
86  skip = 1
87  else:
88  value += chr(input[i])
89  return value
90 
91 
92 def d_value(state, input, index, length, data):
93  if(state == id.SEM_PRE):
94  data['value'] = string_eval(input[index:index + length])
95 
96 
97 def s_value(state, input, index, length, data):
98  if(state == id.SEM_PRE):
99  data['value'] = string_eval(input[index:index + length])
100 
101 
102 def string_value(state, input, index, length, data):
103  if(state == id.SEM_PRE):
104  data['value'] = string_eval(input[index:index + length])
105 
106 
107 def number_value(state, input, index, length, data):
108  if(state == id.SEM_PRE):
109  value = utils.tuple_to_string(input[index:index + length])
110  data['value'] = int(value)
111 
112 
113 def true_value(state, input, index, length, data):
114  if(state == id.SEM_PRE):
115  data['value'] = True
116 
117 
118 def false_value(state, input, index, length, data):
119  if(state == id.SEM_PRE):
120  data['value'] = False
121 
122 
123 def null_value(state, input, index, length, data):
124  if(state == id.SEM_PRE):
125  data['value'] = None
def d_value(state, input, index, length, data)
def key_name(state, input, index, length, data)
def null_value(state, input, index, length, data)
def true_value(state, input, index, length, data)
def number_value(state, input, index, length, data)
def string_value(state, input, index, length, data)
def section_name(state, input, index, length, data)
def s_value(state, input, index, length, data)
def false_value(state, input, index, length, data)
def value(state, input, index, length, data)
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.