Przeglądaj źródła

Refactor parser/__init__.py for new parsing mechanism

tags/v1.0^2
Benjamin Attal 10 lat temu
rodzic
commit
3bc748242d
1 zmienionych plików z 10 dodań i 27 usunięć
  1. +10
    -27
      bitshift/parser/__init__.py

+ 10
- 27
bitshift/parser/__init__.py Wyświetl plik

@@ -1,40 +1,23 @@
from .python import parse_py
from .c import parse_c
from ..languages import LANGS
import pygments.lexers as pgl
import os

_all__ = ["parse"]

def _lang(codelet):
def parse(codelet):
"""
Private function to identify the language of a codelet.
Sends codelet code to the Java parsing process via a named pipe. Reads the
resulting symbols from the pipe and updates the codelet.

:param codelet: The codelet object to identified.
:param codelet: The codelet object to parsed.

:type code: Codelet

.. todo::
Modify function to incorporate tags from stackoverflow.
"""

if codelet.filename is not None:
return pgl.guess_lexer_for_filename(codelet.filename).name

return LANGS.index(pgl.guess_lexer(codelet.code))

def parse(codelet):
"""
Dispatch codelet to correct parser by language of code.
Create a named pipe for python process to communicate with Java
process.

:param codelet: The codelet object to parsed.
Send the process id and codelet code through the named pipe.

:type code: Codelet
Read the result from the named pipe and turn it into a dict.
"""

lang = _lang(codelet)

if lang == LANGS.index("Python"):
parse_py(codelet)
elif lang == LANGS.index("C"):
parse_c(codelet)
pass


Ładowanie…
Anuluj
Zapisz