-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve requirements and environment markers parsing (#44)
- Loading branch information
Showing
48 changed files
with
6,619 additions
and
712 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright © 2017 Erez Shinan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .tree import Tree | ||
from .visitors import Transformer, Visitor, v_args, Discard | ||
from .visitors import InlineTransformer, inline_args # XXX Deprecated | ||
from .exceptions import (ParseError, LexError, GrammarError, UnexpectedToken, | ||
UnexpectedInput, UnexpectedCharacters, LarkError) | ||
from .lexer import Token | ||
from .lark import Lark | ||
|
||
__version__ = "0.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# For usage of lark with PyInstaller. See https://pyinstaller-sample-hook.readthedocs.io/en/latest/index.html | ||
|
||
import os | ||
|
||
def get_hook_dirs(): | ||
return [os.path.dirname(__file__)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#----------------------------------------------------------------------------- | ||
# Copyright (c) 2017-2020, PyInstaller Development Team. | ||
# | ||
# Distributed under the terms of the GNU General Public License (version 2 | ||
# or later) with exception for distributing the bootloader. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
# | ||
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception) | ||
#----------------------------------------------------------------------------- | ||
|
||
from PyInstaller.utils.hooks import collect_data_files | ||
|
||
datas = collect_data_files('lark') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from .utils import Serialize | ||
from .lexer import TerminalDef | ||
|
||
###{standalone | ||
|
||
class LexerConf(Serialize): | ||
__serialize_fields__ = 'tokens', 'ignore', 'g_regex_flags' | ||
__serialize_namespace__ = TerminalDef, | ||
|
||
def __init__(self, tokens, ignore=(), postlex=None, callbacks=None, g_regex_flags=0): | ||
self.tokens = tokens | ||
self.ignore = ignore | ||
self.postlex = postlex | ||
self.callbacks = callbacks or {} | ||
self.g_regex_flags = g_regex_flags | ||
|
||
def _deserialize(self): | ||
self.callbacks = {} # TODO | ||
|
||
###} | ||
|
||
class ParserConf: | ||
def __init__(self, rules, callbacks, start): | ||
assert isinstance(start, list) | ||
self.rules = rules | ||
self.callbacks = callbacks | ||
self.start = start | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from .utils import STRING_TYPE | ||
|
||
###{standalone | ||
class LarkError(Exception): | ||
pass | ||
|
||
class GrammarError(LarkError): | ||
pass | ||
|
||
class ParseError(LarkError): | ||
pass | ||
|
||
class LexError(LarkError): | ||
pass | ||
|
||
class UnexpectedEOF(ParseError): | ||
def __init__(self, expected): | ||
self.expected = expected | ||
|
||
message = ("Unexpected end-of-input. Expected one of: \n\t* %s\n" % '\n\t* '.join(x.name for x in self.expected)) | ||
super(UnexpectedEOF, self).__init__(message) | ||
|
||
|
||
class UnexpectedInput(LarkError): | ||
pos_in_stream = None | ||
|
||
def get_context(self, text, span=40): | ||
pos = self.pos_in_stream | ||
start = max(pos - span, 0) | ||
end = pos + span | ||
before = text[start:pos].rsplit('\n', 1)[-1] | ||
after = text[pos:end].split('\n', 1)[0] | ||
return before + after + '\n' + ' ' * len(before) + '^\n' | ||
|
||
def match_examples(self, parse_fn, examples, token_type_match_fallback=False): | ||
""" Given a parser instance and a dictionary mapping some label with | ||
some malformed syntax examples, it'll return the label for the | ||
example that bests matches the current error. | ||
""" | ||
assert self.state is not None, "Not supported for this exception" | ||
|
||
candidate = (None, False) | ||
for label, example in examples.items(): | ||
assert not isinstance(example, STRING_TYPE) | ||
|
||
for malformed in example: | ||
try: | ||
parse_fn(malformed) | ||
except UnexpectedInput as ut: | ||
if ut.state == self.state: | ||
try: | ||
if ut.token == self.token: # Try exact match first | ||
return label | ||
|
||
if token_type_match_fallback: | ||
# Fallback to token types match | ||
if (ut.token.type == self.token.type) and not candidate[-1]: | ||
candidate = label, True | ||
|
||
except AttributeError: | ||
pass | ||
if not candidate[0]: | ||
candidate = label, False | ||
|
||
return candidate[0] | ||
|
||
|
||
class UnexpectedCharacters(LexError, UnexpectedInput): | ||
def __init__(self, seq, lex_pos, line, column, allowed=None, considered_tokens=None, state=None, token_history=None): | ||
message = "No terminal defined for '%s' at line %d col %d" % (seq[lex_pos], line, column) | ||
|
||
self.line = line | ||
self.column = column | ||
self.allowed = allowed | ||
self.considered_tokens = considered_tokens | ||
self.pos_in_stream = lex_pos | ||
self.state = state | ||
|
||
message += '\n\n' + self.get_context(seq) | ||
if allowed: | ||
message += '\nExpecting: %s\n' % allowed | ||
if token_history: | ||
message += '\nPrevious tokens: %s\n' % ', '.join(repr(t) for t in token_history) | ||
|
||
super(UnexpectedCharacters, self).__init__(message) | ||
|
||
|
||
|
||
class UnexpectedToken(ParseError, UnexpectedInput): | ||
def __init__(self, token, expected, considered_rules=None, state=None, puppet=None): | ||
self.token = token | ||
self.expected = expected # XXX str shouldn't necessary | ||
self.line = getattr(token, 'line', '?') | ||
self.column = getattr(token, 'column', '?') | ||
self.considered_rules = considered_rules | ||
self.state = state | ||
self.pos_in_stream = getattr(token, 'pos_in_stream', None) | ||
self.puppet = puppet | ||
|
||
message = ("Unexpected token %r at line %s, column %s.\n" | ||
"Expected one of: \n\t* %s\n" | ||
% (token, self.line, self.column, '\n\t* '.join(self.expected))) | ||
|
||
super(UnexpectedToken, self).__init__(message) | ||
|
||
class VisitError(LarkError): | ||
"""VisitError is raised when visitors are interrupted by an exception | ||
It provides the following attributes for inspection: | ||
- obj: the tree node or token it was processing when the exception was raised | ||
- orig_exc: the exception that cause it to fail | ||
""" | ||
def __init__(self, rule, obj, orig_exc): | ||
self.obj = obj | ||
self.orig_exc = orig_exc | ||
|
||
message = 'Error trying to process rule "%s":\n\n%s' % (rule, orig_exc) | ||
super(VisitError, self).__init__(message) | ||
###} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from .utils import Serialize | ||
|
||
###{standalone | ||
|
||
class Symbol(Serialize): | ||
__slots__ = ('name',) | ||
|
||
is_term = NotImplemented | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
def __eq__(self, other): | ||
assert isinstance(other, Symbol), other | ||
return self.is_term == other.is_term and self.name == other.name | ||
|
||
def __ne__(self, other): | ||
return not (self == other) | ||
|
||
def __hash__(self): | ||
return hash(self.name) | ||
|
||
def __repr__(self): | ||
return '%s(%r)' % (type(self).__name__, self.name) | ||
|
||
fullrepr = property(__repr__) | ||
|
||
|
||
class Terminal(Symbol): | ||
__serialize_fields__ = 'name', 'filter_out' | ||
|
||
is_term = True | ||
|
||
def __init__(self, name, filter_out=False): | ||
self.name = name | ||
self.filter_out = filter_out | ||
|
||
@property | ||
def fullrepr(self): | ||
return '%s(%r, %r)' % (type(self).__name__, self.name, self.filter_out) | ||
|
||
|
||
|
||
class NonTerminal(Symbol): | ||
__serialize_fields__ = 'name', | ||
|
||
is_term = False | ||
|
||
|
||
|
||
class RuleOptions(Serialize): | ||
__serialize_fields__ = 'keep_all_tokens', 'expand1', 'priority', 'template_source', 'empty_indices' | ||
|
||
def __init__(self, keep_all_tokens=False, expand1=False, priority=None, template_source=None, empty_indices=()): | ||
self.keep_all_tokens = keep_all_tokens | ||
self.expand1 = expand1 | ||
self.priority = priority | ||
self.template_source = template_source | ||
self.empty_indices = empty_indices | ||
|
||
def __repr__(self): | ||
return 'RuleOptions(%r, %r, %r, %r)' % ( | ||
self.keep_all_tokens, | ||
self.expand1, | ||
self.priority, | ||
self.template_source | ||
) | ||
|
||
|
||
class Rule(Serialize): | ||
""" | ||
origin : a symbol | ||
expansion : a list of symbols | ||
order : index of this expansion amongst all rules of the same name | ||
""" | ||
__slots__ = ('origin', 'expansion', 'alias', 'options', 'order', '_hash') | ||
|
||
__serialize_fields__ = 'origin', 'expansion', 'order', 'alias', 'options' | ||
__serialize_namespace__ = Terminal, NonTerminal, RuleOptions | ||
|
||
def __init__(self, origin, expansion, order=0, alias=None, options=None): | ||
self.origin = origin | ||
self.expansion = expansion | ||
self.alias = alias | ||
self.order = order | ||
self.options = options or RuleOptions() | ||
self._hash = hash((self.origin, tuple(self.expansion))) | ||
|
||
def _deserialize(self): | ||
self._hash = hash((self.origin, tuple(self.expansion))) | ||
|
||
def __str__(self): | ||
return '<%s : %s>' % (self.origin.name, ' '.join(x.name for x in self.expansion)) | ||
|
||
def __repr__(self): | ||
return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options) | ||
|
||
def __hash__(self): | ||
return self._hash | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, Rule): | ||
return False | ||
return self.origin == other.origin and self.expansion == other.expansion | ||
|
||
|
||
|
||
###} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Numbers | ||
// | ||
|
||
DIGIT: "0".."9" | ||
HEXDIGIT: "a".."f"|"A".."F"|DIGIT | ||
|
||
INT: DIGIT+ | ||
SIGNED_INT: ["+"|"-"] INT | ||
DECIMAL: INT "." INT? | "." INT | ||
|
||
// float = /-?\d+(\.\d+)?([eE][+-]?\d+)?/ | ||
_EXP: ("e"|"E") SIGNED_INT | ||
FLOAT: INT _EXP | DECIMAL _EXP? | ||
SIGNED_FLOAT: ["+"|"-"] FLOAT | ||
|
||
NUMBER: FLOAT | INT | ||
SIGNED_NUMBER: ["+"|"-"] NUMBER | ||
|
||
// | ||
// Strings | ||
// | ||
_STRING_INNER: /.*?/ | ||
_STRING_ESC_INNER: _STRING_INNER /(?<!\\)(\\\\)*?/ | ||
|
||
ESCAPED_STRING : "\"" _STRING_ESC_INNER "\"" | ||
|
||
|
||
// | ||
// Names (Variables) | ||
// | ||
LCASE_LETTER: "a".."z" | ||
UCASE_LETTER: "A".."Z" | ||
|
||
LETTER: UCASE_LETTER | LCASE_LETTER | ||
WORD: LETTER+ | ||
|
||
CNAME: ("_"|LETTER) ("_"|LETTER|DIGIT)* | ||
|
||
|
||
// | ||
// Whitespace | ||
// | ||
WS_INLINE: (" "|/\t/)+ | ||
WS: /[ \t\f\r\n]/+ | ||
|
||
CR : /\r/ | ||
LF : /\n/ | ||
NEWLINE: (CR? LF)+ | ||
|
Oops, something went wrong.