Commit 6980e149 authored by Robert Bradshaw's avatar Robert Bradshaw

Handle includes in new parser.

parent 919d24f6
...@@ -6,12 +6,12 @@ cdef extern from "graminit.c": ...@@ -6,12 +6,12 @@ cdef extern from "graminit.c":
cdef extern from "node.h": cdef extern from "node.h":
ctypedef struct node ctypedef struct node
void PyNode_Free(node*) void PyNode_Free(node* n)
int NCH(node*) int NCH(node* n)
node* CHILD(node*, int) node* CHILD(node* n, int ix)
node* RCHILD(node*, int) node* RCHILD(node* n, int ix)
short TYPE(node*) short TYPE(node* n)
char* STR(node*) char* STR(node* n)
cdef extern from "parsetok.h": cdef extern from "parsetok.h":
ctypedef struct perrdetail: ctypedef struct perrdetail:
...@@ -27,6 +27,7 @@ cdef extern from "parsetok.h": ...@@ -27,6 +27,7 @@ cdef extern from "parsetok.h":
import distutils.sysconfig import distutils.sysconfig
import os import os
import re
def extract_names(path): def extract_names(path):
# All parse tree types are #defined in these files as ints. # All parse tree types are #defined in these files as ints.
...@@ -54,11 +55,24 @@ cdef print_tree(node* n, indent=""): ...@@ -54,11 +55,24 @@ cdef print_tree(node* n, indent=""):
for i in range(NCH(n)): for i in range(NCH(n)):
print_tree(CHILD(n, i), indent) print_tree(CHILD(n, i), indent)
def handle_includes(source, path):
# TODO: Use include directory.
def include_here(include_line):
included = os.path.join(os.path.dirname(path), include_line.group(1)[1:-1])
if not os.path.exists(included):
return include_line.group(0) + ' # no such path: ' + included
return handle_includes(open(included).read(), path)
return re.sub(r'^include\s+([^\n]+)\s+(#.*)?$', include_here, source, flags=re.M)
def p_module(path): def p_module(path):
cdef perrdetail err cdef perrdetail err
cdef int flags cdef int flags
cdef node* n cdef node* n
source = open(path).read() source = open(path).read()
if '\ninclude ' in source:
# TODO: Tokanizer needs to understand includes.
source = handle_includes(source, path)
path = "preparse(%s)" % path
n = PyParser_ParseStringFlagsFilenameEx( n = PyParser_ParseStringFlagsFilenameEx(
source, source,
path, path,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment