| 1 |
from typing import Callable, Union |
| 2 |
from typing_extensions import Literal |
| 3 |
from .errors import ParseError |
| 4 |
|
| 5 |
|
| 6 |
class ParserStream: |
| 7 |
def __init__(self, string: str): |
| 8 |
self.string = string |
| 9 |
self.index = 0 |
| 10 |
self.peek_offset = 0 |
| 11 |
|
| 12 |
def get(self, offset: int) -> Union[str, None]: |
| 13 |
try: |
| 14 |
return self.string[offset] |
| 15 |
except IndexError: |
| 16 |
return None |
| 17 |
|
| 18 |
def char_at(self, offset: int) -> Union[str, None]: |
| 19 |
# When the cursor is at CRLF, return LF but don't move the cursor. The |
| 20 |
# cursor still points to the EOL position, which in this case is the |
| 21 |
# beginning of the compound CRLF sequence. This ensures slices of |
| 22 |
# [inclusive, exclusive) continue to work properly. |
| 23 |
if self.get(offset) == '\r' \ |
| 24 |
and self.get(offset + 1) == '\n': |
| 25 |
return '\n' |
| 26 |
|
| 27 |
return self.get(offset) |
| 28 |
|
| 29 |
@property |
| 30 |
def current_char(self) -> Union[str, None]: |
| 31 |
return self.char_at(self.index) |
| 32 |
|
| 33 |
@property |
| 34 |
def current_peek(self) -> Union[str, None]: |
| 35 |
return self.char_at(self.index + self.peek_offset) |
| 36 |
|
| 37 |
def next(self) -> Union[str, None]: |
| 38 |
self.peek_offset = 0 |
| 39 |
# Skip over CRLF as if it was a single character. |
| 40 |
if self.get(self.index) == '\r' \ |
| 41 |
and self.get(self.index + 1) == '\n': |
| 42 |
self.index += 1 |
| 43 |
self.index += 1 |
| 44 |
return self.get(self.index) |
| 45 |
|
| 46 |
def peek(self) -> Union[str, None]: |
| 47 |
# Skip over CRLF as if it was a single character. |
| 48 |
if self.get(self.index + self.peek_offset) == '\r' \ |
| 49 |
and self.get(self.index + self.peek_offset + 1) == '\n': |
| 50 |
self.peek_offset += 1 |
| 51 |
self.peek_offset += 1 |
| 52 |
return self.get(self.index + self.peek_offset) |
| 53 |
|
| 54 |
def reset_peek(self, offset: int = 0) -> None: |
| 55 |
self.peek_offset = offset |
| 56 |
|
| 57 |
def skip_to_peek(self) -> None: |
| 58 |
self.index += self.peek_offset |
| 59 |
self.peek_offset = 0 |
| 60 |
|
| 61 |
|
| 62 |
EOL = '\n' |
| 63 |
EOF = None |
| 64 |
SPECIAL_LINE_START_CHARS = ('}', '.', '[', '*') |
| 65 |
|
| 66 |
|
| 67 |
class FluentParserStream(ParserStream): |
| 68 |
|
| 69 |
def peek_blank_inline(self) -> str: |
| 70 |
start = self.index + self.peek_offset |
| 71 |
while self.current_peek == ' ': |
| 72 |
self.peek() |
| 73 |
return self.string[start:self.index + self.peek_offset] |
| 74 |
|
| 75 |
def skip_blank_inline(self) -> str: |
| 76 |
blank = self.peek_blank_inline() |
| 77 |
self.skip_to_peek() |
| 78 |
return blank |
| 79 |
|
| 80 |
def peek_blank_block(self) -> str: |
| 81 |
blank = "" |
| 82 |
while True: |
| 83 |
line_start = self.peek_offset |
| 84 |
self.peek_blank_inline() |
| 85 |
|
| 86 |
if self.current_peek == EOL: |
| 87 |
blank += EOL |
| 88 |
self.peek() |
| 89 |
continue |
| 90 |
|
| 91 |
if self.current_peek is EOF: |
| 92 |
# Treat the blank line at EOF as a blank block. |
| 93 |
return blank |
| 94 |
|
| 95 |
# Any other char; reset to column 1 on this line. |
| 96 |
self.reset_peek(line_start) |
| 97 |
return blank |
| 98 |
|
| 99 |
def skip_blank_block(self) -> str: |
| 100 |
blank = self.peek_blank_block() |
| 101 |
self.skip_to_peek() |
| 102 |
return blank |
| 103 |
|
| 104 |
def peek_blank(self) -> None: |
| 105 |
while self.current_peek in (" ", EOL): |
| 106 |
self.peek() |
| 107 |
|
| 108 |
def skip_blank(self) -> None: |
| 109 |
self.peek_blank() |
| 110 |
self.skip_to_peek() |
| 111 |
|
| 112 |
def expect_char(self, ch: str) -> Literal[True]: |
| 113 |
if self.current_char == ch: |
| 114 |
self.next() |
| 115 |
return True |
| 116 |
|
| 117 |
raise ParseError('E0003', ch) |
| 118 |
|
| 119 |
def expect_line_end(self) -> Literal[True]: |
| 120 |
if self.current_char is EOF: |
| 121 |
# EOF is a valid line end in Fluent. |
| 122 |
return True |
| 123 |
|
| 124 |
if self.current_char == EOL: |
| 125 |
self.next() |
| 126 |
return True |
| 127 |
|
| 128 |
# Unicode Character 'SYMBOL FOR NEWLINE' (U+2424) |
| 129 |
raise ParseError('E0003', '\u2424') |
| 130 |
|
| 131 |
def take_char(self, f: Callable[[str], bool]) -> Union[str, Literal[False], None]: |
| 132 |
ch = self.current_char |
| 133 |
if ch is None: |
| 134 |
return EOF |
| 135 |
if f(ch): |
| 136 |
self.next() |
| 137 |
return ch |
| 138 |
return False |
| 139 |
|
| 140 |
def is_char_id_start(self, ch: Union[str, None]) -> bool: |
| 141 |
if ch is None: |
| 142 |
return False |
| 143 |
|
| 144 |
cc = ord(ch) |
| 145 |
return (cc >= 97 and cc <= 122) or \ |
| 146 |
(cc >= 65 and cc <= 90) |
| 147 |
|
| 148 |
def is_identifier_start(self) -> bool: |
| 149 |
return self.is_char_id_start(self.current_peek) |
| 150 |
|
| 151 |
def is_number_start(self) -> bool: |
| 152 |
ch = self.peek() if self.current_char == '-' else self.current_char |
| 153 |
if ch is None: |
| 154 |
self.reset_peek() |
| 155 |
return False |
| 156 |
|
| 157 |
cc = ord(ch) |
| 158 |
is_digit = cc >= 48 and cc <= 57 |
| 159 |
self.reset_peek() |
| 160 |
return is_digit |
| 161 |
|
| 162 |
def is_char_pattern_continuation(self, ch: Union[str, None]) -> bool: |
| 163 |
if ch is EOF: |
| 164 |
return False |
| 165 |
|
| 166 |
return ch not in SPECIAL_LINE_START_CHARS |
| 167 |
|
| 168 |
def is_value_start(self) -> bool: |
| 169 |
# Inline Patterns may start with any char. |
| 170 |
return self.current_peek is not EOF and self.current_peek != EOL |
| 171 |
|
| 172 |
def is_value_continuation(self) -> bool: |
| 173 |
column1 = self.peek_offset |
| 174 |
self.peek_blank_inline() |
| 175 |
|
| 176 |
if self.current_peek == '{': |
| 177 |
self.reset_peek(column1) |
| 178 |
return True |
| 179 |
|
| 180 |
if self.peek_offset - column1 == 0: |
| 181 |
return False |
| 182 |
|
| 183 |
if self.is_char_pattern_continuation(self.current_peek): |
| 184 |
self.reset_peek(column1) |
| 185 |
return True |
| 186 |
|
| 187 |
return False |
| 188 |
|
| 189 |
# -1 - any |
| 190 |
# 0 - comment |
| 191 |
# 1 - group comment |
| 192 |
# 2 - resource comment |
| 193 |
def is_next_line_comment(self, level: int = -1) -> bool: |
| 194 |
if self.current_peek != EOL: |
| 195 |
return False |
| 196 |
|
| 197 |
i = 0 |
| 198 |
|
| 199 |
while (i <= level or (level == -1 and i < 3)): |
| 200 |
if self.peek() != '#': |
| 201 |
if i <= level and level != -1: |
| 202 |
self.reset_peek() |
| 203 |
return False |
| 204 |
break |
| 205 |
i += 1 |
| 206 |
|
| 207 |
# The first char after #, ## or ###. |
| 208 |
if self.peek() in (' ', EOL): |
| 209 |
self.reset_peek() |
| 210 |
return True |
| 211 |
|
| 212 |
self.reset_peek() |
| 213 |
return False |
| 214 |
|
| 215 |
def is_variant_start(self) -> bool: |
| 216 |
current_peek_offset = self.peek_offset |
| 217 |
if self.current_peek == '*': |
| 218 |
self.peek() |
| 219 |
if self.current_peek == '[' and self.peek() != '[': |
| 220 |
self.reset_peek(current_peek_offset) |
| 221 |
return True |
| 222 |
|
| 223 |
self.reset_peek(current_peek_offset) |
| 224 |
return False |
| 225 |
|
| 226 |
def is_attribute_start(self) -> bool: |
| 227 |
return self.current_peek == '.' |
| 228 |
|
| 229 |
def skip_to_next_entry_start(self, junk_start: int) -> None: |
| 230 |
last_newline = self.string.rfind(EOL, 0, self.index) |
| 231 |
if junk_start < last_newline: |
| 232 |
# Last seen newline is _after_ the junk start. It's safe to rewind |
| 233 |
# without the risk of resuming at the same broken entry. |
| 234 |
self.index = last_newline |
| 235 |
|
| 236 |
while self.current_char: |
| 237 |
# We're only interested in beginnings of line. |
| 238 |
if self.current_char != EOL: |
| 239 |
self.next() |
| 240 |
continue |
| 241 |
|
| 242 |
# Break if the first char in this line looks like an entry start. |
| 243 |
first = self.next() |
| 244 |
if self.is_char_id_start(first) or first == '-' or first == '#': |
| 245 |
break |
| 246 |
|
| 247 |
# Syntax 0.4 compatibility |
| 248 |
peek = self.peek() |
| 249 |
self.reset_peek() |
| 250 |
if (first, peek) == ('/', '/') or (first, peek) == ('[', '['): |
| 251 |
break |
| 252 |
|
| 253 |
def take_id_start(self) -> Union[str, None]: |
| 254 |
if self.is_char_id_start(self.current_char): |
| 255 |
ret = self.current_char |
| 256 |
self.next() |
| 257 |
return ret |
| 258 |
|
| 259 |
raise ParseError('E0004', 'a-zA-Z') |
| 260 |
|
| 261 |
def take_id_char(self) -> Union[str, Literal[False], None]: |
| 262 |
def closure(ch: str) -> bool: |
| 263 |
cc = ord(ch) |
| 264 |
return ((cc >= 97 and cc <= 122) or |
| 265 |
(cc >= 65 and cc <= 90) or |
| 266 |
(cc >= 48 and cc <= 57) or |
| 267 |
cc == 95 or cc == 45) |
| 268 |
return self.take_char(closure) |
| 269 |
|
| 270 |
def take_digit(self) -> Union[str, Literal[False], None]: |
| 271 |
def closure(ch: str) -> bool: |
| 272 |
cc = ord(ch) |
| 273 |
return (cc >= 48 and cc <= 57) |
| 274 |
return self.take_char(closure) |
| 275 |
|
| 276 |
def take_hex_digit(self) -> Union[str, Literal[False], None]: |
| 277 |
def closure(ch: str) -> bool: |
| 278 |
cc = ord(ch) |
| 279 |
return ( |
| 280 |
(cc >= 48 and cc <= 57) # 0-9 |
| 281 |
or (cc >= 65 and cc <= 70) # A-F |
| 282 |
or (cc >= 97 and cc <= 102)) # a-f |
| 283 |
return self.take_char(closure) |