44character escape sequences.
55"""
66
7- from typing import Callable
7+ from typing import List
88
99from mathics_scanner .characters import named_characters
1010from mathics_scanner .errors import ScanError , IncompleteSyntaxError
11+ from mathics_scanner .feed import LineFeeder
1112
1213
1314class Prescanner (object ):
@@ -27,11 +28,15 @@ class Prescanner(object):
2728 Trailing backslash characters (\) are reported incomplete.
2829 """
2930
30- def __init__ (self , feeder : Callable ):
31- # feeder is a function that returns the next line of the Mathics input
31+ def __init__ (self , feeder : LineFeeder ):
32+ # self. feeder is a function that returns the next line of the Mathics input
3233 self .feeder = feeder
33- self .code = feeder .feed () # input code
34- self .pos = 0 # current position within code
34+
35+ # self.input_line is the result of reading the next Mathics input line
36+ self .input_line : str = feeder .feed ()
37+
38+ # self.pos is current position within self.input_line.
39+ self .pos = 0
3540
3641 def feed (self ) -> str :
3742 """
@@ -40,20 +45,20 @@ def feed(self) -> str:
4045 return self .feeder .feed ()
4146
4247 def incomplete (self ):
43- line = self .feed ()
48+ line : str = self .feed ()
4449 if not line :
45- self .feeder .message ("Syntax" , "sntxi" , self .code [self .pos :].rstrip ())
50+ self .feeder .message ("Syntax" , "sntxi" , self .input_line [self .pos :].rstrip ())
4651 raise IncompleteSyntaxError ()
47- self .code += line
52+ self .input_line += line
4853
4954 def replace_escape_sequences (self ) -> str :
5055 """
51- Replace escape sequences in self.code . The replacement string is returned.
52- Note: self.code is not modified.
56+ Replace escape sequences in `` self.input_line`` . The replacement string is returned.
57+ Note: `` self.input_line`` is not modified.
5358 """
5459
5560 # Line fragments to be joined before returning from this method.
56- line_fragments = []
61+ line_fragments : List [ str ] = []
5762
5863 # Fragment start position of line fragment under consideration.
5964 self .fragment_start = self .pos
@@ -66,7 +71,7 @@ def start_new_fragment(pos: int) -> None:
6671 self .fragment_start = pos
6772
6873 def try_parse_base (start_shift : int , end_shift : int , base : int ) -> None :
69- """
74+ r """
7075 See if characters self.pos+start_shift .. self.pos+end shift
7176 can be converted to an integer in base ``base``.
7277
@@ -81,8 +86,8 @@ def try_parse_base(start_shift: int, end_shift: int, base: int) -> None:
8186 """
8287 start , end = self .pos + start_shift , self .pos + end_shift
8388 result = None
84- if end <= len (self .code ):
85- text = self .code [start :end ]
89+ if end <= len (self .input_line ):
90+ text = self .input_line [start :end ]
8691 try :
8792 result = int (text , base )
8893 except ValueError :
@@ -98,21 +103,21 @@ def try_parse_base(start_shift: int, end_shift: int, base: int) -> None:
98103 else :
99104 raise ValueError ()
100105 self .feeder .message (
101- "Syntax" , "sntxb" , self .code [self .pos :].rstrip ("\n " )
106+ "Syntax" , "sntxb" , self .input_line [self .pos :].rstrip ("\n " )
102107 )
103108 raise ScanError ()
104109
105110 # Add text from prior line fragment as well
106111 # as the escape sequence, a character, from the escape sequence
107112 # that was just matched.
108- line_fragments .append (self .code [start : self .pos ])
113+ line_fragments .append (self .input_line [start : self .pos ])
109114 line_fragments .append (chr (result ))
110115
111116 # Set up a new line fragment for the next time we are called.
112117 start_new_fragment (end )
113118
114119 def try_parse_named_character (start_shift : int ):
115- """Before calling we have matched "\[". Scan to the remaining "]" and
120+ r """Before calling we have matched "\[". Scan to the remaining "]" and
116121 try to match what is found in-between with a known named
117122 character, e.g. "Theta". If we can match this, we store
118123 the unicode character equivalent in ``line_fragments``.
@@ -121,13 +126,13 @@ def try_parse_named_character(start_shift: int):
121126 """
122127 i = self .pos + start_shift
123128 while True :
124- if i == len (self .code ):
129+ if i == len (self .input_line ):
125130 self .incomplete ()
126- if self .code [i ] == "]" :
131+ if self .input_line [i ] == "]" :
127132 break
128133 i += 1
129134
130- named_character = self .code [self .pos + start_shift : i ]
135+ named_character = self .input_line [self .pos + start_shift : i ]
131136 if named_character .isalpha ():
132137 char = named_characters .get (named_character )
133138 if char is None :
@@ -137,7 +142,9 @@ def try_parse_named_character(start_shift: int):
137142 # Add text from prior line fragment as well
138143 # as the escape sequence, a character, from the escape sequence
139144 # just matched.
140- line_fragments .append (self .code [self .fragment_start : self .pos ])
145+ line_fragments .append (
146+ self .input_line [self .fragment_start : self .pos ]
147+ )
141148 line_fragments .append (char )
142149 start_new_fragment (i + 1 )
143150
@@ -151,12 +158,12 @@ def try_parse_named_character(start_shift: int):
151158 # stored in ``line_fragments``. The start-position marker for the
152159 # next line_fragment is started and self.pos is updated.
153160
154- while self .pos < len (self .code ):
155- if self .code [self .pos ] == "\\ " :
161+ while self .pos < len (self .input_line ):
162+ if self .input_line [self .pos ] == "\\ " :
156163 # Look for and handle an escape sequence.
157- if self .pos + 1 == len (self .code ):
164+ if self .pos + 1 == len (self .input_line ):
158165 self .incomplete ()
159- c = self .code [self .pos + 1 ]
166+ c = self .input_line [self .pos + 1 ]
160167 if c == "|" :
161168 try_parse_base (2 , 8 , 16 )
162169 if c == "." :
@@ -171,10 +178,10 @@ def try_parse_named_character(start_shift: int):
171178 # See if we have an octal number.
172179 try_parse_base (1 , 4 , 8 )
173180 elif c == "\n " :
174- if self .pos + 2 == len (self .code ):
181+ if self .pos + 2 == len (self .input_line ):
175182 self .incomplete ()
176- self . line_fragments .append (
177- self .code [self .fragment_start : self .pos ]
183+ line_fragments .append (
184+ self .input_line [self .fragment_start : self .pos ]
178185 )
179186 start_new_fragment (self .pos + 2 )
180187 else :
@@ -187,7 +194,7 @@ def try_parse_named_character(start_shift: int):
187194 self .pos += 1
188195
189196 # Add the final line fragment.
190- line_fragments .append (self .code [self .fragment_start :])
197+ line_fragments .append (self .input_line [self .fragment_start :])
191198
192- # produce and return the replacement string.
199+ # Produce and return the input line with escape-sequences replaced
193200 return "" .join (line_fragments )
0 commit comments