mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Move some more stuff to a python directory in the parser.
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
from jedi.parser.parser import Parser, ParserWithRecovery, load_grammar, \
|
from jedi.parser.parser import Parser, ParserWithRecovery, ParseError
|
||||||
ParseError
|
from jedi.parser.python import load_grammar
|
||||||
|
|||||||
@@ -15,49 +15,21 @@ within the statement. This lowers memory usage and cpu time and reduces the
|
|||||||
complexity of the ``Parser`` (there's another parser sitting inside
|
complexity of the ``Parser`` (there's another parser sitting inside
|
||||||
``Statement``, which produces ``Array`` and ``Call``).
|
``Statement``, which produces ``Array`` and ``Call``).
|
||||||
"""
|
"""
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from jedi._compatibility import FileNotFoundError
|
|
||||||
from jedi.parser import tree as pt
|
from jedi.parser import tree as pt
|
||||||
from jedi.parser import tokenize
|
from jedi.parser import tokenize
|
||||||
from jedi.parser.token import (DEDENT, INDENT, ENDMARKER, NEWLINE, NUMBER,
|
from jedi.parser.token import (DEDENT, INDENT, ENDMARKER, NEWLINE, NUMBER,
|
||||||
STRING, tok_name)
|
STRING, tok_name)
|
||||||
from jedi.parser.pgen2.pgen import generate_grammar
|
|
||||||
from jedi.parser.pgen2.parse import PgenParser
|
from jedi.parser.pgen2.parse import PgenParser
|
||||||
|
|
||||||
|
|
||||||
_loaded_grammars = {}
|
|
||||||
|
|
||||||
|
|
||||||
class ParseError(Exception):
|
class ParseError(Exception):
|
||||||
"""
|
"""
|
||||||
Signals you that the code you fed the Parser was not correct Python code.
|
Signals you that the code you fed the Parser was not correct Python code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def load_grammar(version='3.6'):
|
|
||||||
# For now we only support two different Python syntax versions: The latest
|
|
||||||
# Python 3 and Python 2. This may change.
|
|
||||||
if version in ('3.2', '3.3'):
|
|
||||||
version = '3.4'
|
|
||||||
elif version == '2.6':
|
|
||||||
version = '2.7'
|
|
||||||
|
|
||||||
file = 'grammar' + version + '.txt'
|
|
||||||
|
|
||||||
global _loaded_grammars
|
|
||||||
path = os.path.join(os.path.dirname(__file__), file)
|
|
||||||
try:
|
|
||||||
return _loaded_grammars[path]
|
|
||||||
except KeyError:
|
|
||||||
try:
|
|
||||||
return _loaded_grammars.setdefault(path, generate_grammar(path))
|
|
||||||
except FileNotFoundError:
|
|
||||||
# Just load the default if the file does not exist.
|
|
||||||
return load_grammar()
|
|
||||||
|
|
||||||
|
|
||||||
class ParserSyntaxError(object):
|
class ParserSyntaxError(object):
|
||||||
def __init__(self, message, position):
|
def __init__(self, message, position):
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|||||||
43
jedi/parser/python/__init__.py
Normal file
43
jedi/parser/python/__init__.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from jedi._compatibility import FileNotFoundError
|
||||||
|
from jedi.parser.pgen2.pgen import generate_grammar
|
||||||
|
from jedi.parser.parser import Parser, ParserWithRecovery
|
||||||
|
from jedi.parser.tokenize import source_tokens
|
||||||
|
|
||||||
|
|
||||||
|
_loaded_grammars = {}
|
||||||
|
|
||||||
|
|
||||||
|
def load_grammar(version='3.6'):
|
||||||
|
# For now we only support two different Python syntax versions: The latest
|
||||||
|
# Python 3 and Python 2. This may change.
|
||||||
|
if version in ('3.2', '3.3'):
|
||||||
|
version = '3.4'
|
||||||
|
elif version == '2.6':
|
||||||
|
version = '2.7'
|
||||||
|
|
||||||
|
file = 'grammar' + version + '.txt'
|
||||||
|
|
||||||
|
global _loaded_grammars
|
||||||
|
path = os.path.join(os.path.dirname(__file__), file)
|
||||||
|
try:
|
||||||
|
return _loaded_grammars[path]
|
||||||
|
except KeyError:
|
||||||
|
try:
|
||||||
|
return _loaded_grammars.setdefault(path, generate_grammar(path))
|
||||||
|
except FileNotFoundError:
|
||||||
|
# Just load the default if the file does not exist.
|
||||||
|
return load_grammar()
|
||||||
|
|
||||||
|
|
||||||
|
def parse(source, grammar=None, error_recovery=False):
|
||||||
|
if grammar is None:
|
||||||
|
grammar = load_grammar()
|
||||||
|
|
||||||
|
tokens = source_tokens(source)
|
||||||
|
if error_recovery:
|
||||||
|
parser = ParserWithRecovery
|
||||||
|
else:
|
||||||
|
parser = Parser
|
||||||
|
parser(grammar, tokens)
|
||||||
2
setup.py
2
setup.py
@@ -11,7 +11,7 @@ __AUTHOR__ = 'David Halter'
|
|||||||
__AUTHOR_EMAIL__ = 'davidhalter88@gmail.com'
|
__AUTHOR_EMAIL__ = 'davidhalter88@gmail.com'
|
||||||
|
|
||||||
readme = open('README.rst').read() + '\n\n' + open('CHANGELOG.rst').read()
|
readme = open('README.rst').read() + '\n\n' + open('CHANGELOG.rst').read()
|
||||||
packages = ['jedi', 'jedi.parser', 'jedi.parser.pgen2',
|
packages = ['jedi', 'jedi.parser', 'jedi.parser.pgen2', 'jedi.parser.python',
|
||||||
'jedi.evaluate', 'jedi.evaluate.compiled', 'jedi.api']
|
'jedi.evaluate', 'jedi.evaluate.compiled', 'jedi.api']
|
||||||
|
|
||||||
import jedi
|
import jedi
|
||||||
|
|||||||
Reference in New Issue
Block a user