Permalink
Browse files

Break another dependency on the 're' module.

Use re2c to translate PLAIN_WORD_RE to a native function, in the exact
same fashion as VAR_NAME_RE -> IsValidVarName().
  • Loading branch information...
Andy Chu
Andy Chu committed May 29, 2018
1 parent 2a65b3c commit d12a555656d55d006d5664b35c70e9bbaf1ca17d
Showing with 26 additions and 10 deletions.
  1. +2 −3 asdl/pretty.py
  2. +0 −2 core/cmd_exec.py
  3. +10 −5 core/lexer_gen.py
  4. +14 −0 native/fastlex.c
View
@@ -9,10 +9,9 @@
fastlex = None
# Word characters, - and _, as well as path name characters . and /.
PLAIN_WORD_RE = r'^[a-zA-Z0-9\-_./]+'
PLAIN_WORD_RE = r'[a-zA-Z0-9\-_./]+'
if 0:
#if fastlex:
if fastlex:
IsPlainWord = fastlex.IsPlainWord
else:
import re
View
@@ -16,8 +16,6 @@
"""
import os
# Used to test whether we should repr() escape
import re
import resource
import sys
import time
View
@@ -5,10 +5,12 @@
"""
import cStringIO
import pprint
import sys
import sre_parse
import sre_constants
from asdl import pretty # For PLAIN_WORD_RE
from osh import lex
from osh import meta
@@ -173,6 +175,8 @@ def TranslateTree(re_tree, f, in_char_class=False):
def TranslateRegex(pat):
re_tree = sre_parse.parse(pat)
# For debugging
#print(pprint.pformat(re_tree), file=sys.stderr)
f = cStringIO.StringIO()
TranslateTree(re_tree, f)
return f.getvalue()
@@ -285,10 +289,10 @@ def TranslateLexer(lexer_def):
}
""")
def TranslateOther(var_name_re):
re2_pat = TranslateRegex(var_name_re)
def TranslateRegexToPredicate(py_regex, func_name):
re2c_pat = TranslateRegex(py_regex)
print(r"""
static inline int IsValidVarName(const char* s, int len) {
static inline int %s(const char* s, int len) {
unsigned char* p = s; /* modified by re2c */
unsigned char* end = s + len;
@@ -298,7 +302,7 @@ def TranslateOther(var_name_re):
* { return 0; }
*/
}
""" % re2_pat)
""" % (func_name, re2c_pat))
# note: use YYCURSOR and YYLIMIT
@@ -312,7 +316,8 @@ def main(argv):
if action == 'c':
# Print code to stdout.
TranslateLexer(lex.LEXER_DEF)
TranslateOther(lex.VAR_NAME_RE)
TranslateRegexToPredicate(lex.VAR_NAME_RE, 'IsValidVarName')
TranslateRegexToPredicate(pretty.PLAIN_WORD_RE, 'IsPlainWord')
elif action == 'print-all':
# Top level is a switch statement.
View
@@ -76,11 +76,25 @@ fastlex_IsValidVarName(PyObject *self, PyObject *args) {
return PyBool_FromLong(IsValidVarName(name, len));
}
static PyObject *
fastlex_IsPlainWord(PyObject *self, PyObject *args) {
const char *name;
int len;
if (!PyArg_ParseTuple(args, "s#", &name, &len)) {
return NULL;
}
return PyBool_FromLong(IsPlainWord(name, len));
}
static PyMethodDef methods[] = {
{"MatchToken", fastlex_MatchToken, METH_VARARGS,
"(lexer mode, line, start_pos) -> (id, end_pos)."},
{"IsValidVarName", fastlex_IsValidVarName, METH_VARARGS,
"Is it a valid var name?"},
{"IsPlainWord", fastlex_IsPlainWord, METH_VARARGS,
"Can the string be pretty-printed without quotes?"},
{NULL, NULL},
};

0 comments on commit d12a555

Please sign in to comment.