Move some more stuff to a python directory in the parser.

This commit is contained in:
Dave Halter
2017-03-13 00:54:39 +01:00
parent 922c480e2e
commit 595ffc24d4
8 changed files with 46 additions and 31 deletions

View File

@@ -1,2 +1,2 @@
from jedi.parser.parser import Parser, ParserWithRecovery, load_grammar, \
ParseError
from jedi.parser.parser import Parser, ParserWithRecovery, ParseError
from jedi.parser.python import load_grammar

View File

@@ -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
``Statement``, which produces ``Array`` and ``Call``).
"""
import os
import re
from jedi._compatibility import FileNotFoundError
from jedi.parser import tree as pt
from jedi.parser import tokenize
from jedi.parser.token import (DEDENT, INDENT, ENDMARKER, NEWLINE, NUMBER,
STRING, tok_name)
from jedi.parser.pgen2.pgen import generate_grammar
from jedi.parser.pgen2.parse import PgenParser
_loaded_grammars = {}
class ParseError(Exception):
"""
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):
def __init__(self, message, position):
self.message = message

View 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)

View File

@@ -11,7 +11,7 @@ __AUTHOR__ = 'David Halter'
__AUTHOR_EMAIL__ = 'davidhalter88@gmail.com'
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']
import jedi