Example #1
0
  """
    from asdl import asdl_

    id_sum = asdl_.Sum([asdl_.Constructor(name) for name, _ in ids])

    variants2 = [asdl_.Constructor(name) for name in id_spec.kind_name_list]
    kind_sum = asdl_.Sum(variants2)

    id_ = asdl_.TypeDecl('Id', id_sum)
    kind_ = asdl_.TypeDecl('Kind', kind_sum)

    schema_ast = asdl_.Module('id_kind', [], [id_, kind_])
    return schema_ast


_BUILTINS = builtin_def.All()


def GenBuiltinLookup(b, func_name, kind, f):
    #log('%r %r', func_name, kind)

    pairs = [(b.name, b.index) for b in _BUILTINS if b.kind == kind]
    #log('%s', pairs)

    groups = collections.defaultdict(list)
    for name, index in pairs:
        first_char = name[0]
        groups[first_char].append((name, index))

    if 0:
        for first_char, pairs in groups.iteritems():
Example #2
0
def main(argv):
    try:
        action = argv[1]
    except IndexError:
        raise RuntimeError('Action required')

    # TODO:
    # generate builtin::echo, etc.
    #
    # And in Python do the same.

    option = _CreateSum(_OPT_ENUM, [opt.name for opt in option_def.All()])
    builtin = _CreateSum(_BUILTIN_ENUM,
                         [b.enum_name for b in builtin_def.All()])
    # TODO: could shrink array later.
    # [opt.name for opt in option_def.All() if opt.implemented])

    schema_ast = asdl_.Module('option', [], [option, builtin])

    if action == 'cpp':
        from asdl import gen_cpp

        out_prefix = argv[2]

        with open(out_prefix + '.h', 'w') as f:
            f.write("""\
#ifndef OPTION_ASDL_H
#define OPTION_ASDL_H

namespace option_asdl {
""")

            # TODO: Could suppress option_str
            v = gen_cpp.ClassDefVisitor(f, {}, simple_int_sums=_SIMPLE)
            v.VisitModule(schema_ast)

            f.write("""
}  // namespace option_asdl

#endif  // OPTION_ASDL_H
""")

        with open(out_prefix + '.cc', 'w') as f:
            f.write("""\
#include <assert.h>
#include "option_asdl.h"

namespace option_asdl {

""")

            v = gen_cpp.MethodDefVisitor(f, {}, simple_int_sums=_SIMPLE)

            v.VisitModule(schema_ast)

            f.write('}  // namespace option_asdl\n')

    elif action == 'mypy':
        from asdl import gen_python

        f = sys.stdout

        f.write("""\
from asdl import pybase

""")
        # option_i type
        v = gen_python.GenMyPyVisitor(f, None, simple_int_sums=_SIMPLE)
        v.VisitModule(schema_ast)

    else:
        raise RuntimeError('Invalid action %r' % action)
Example #3
0
def main(argv):
  # This becomes osh-lexer_def.re2c.c.  It is compiled to osh-lexer_def.c and then
  # included.

  action = argv[1]
  if action == 'c':
    # Print code to stdout.
    TranslateOshLexer(lexer_def.LEXER_DEF)
    TranslateSimpleLexer('MatchEchoToken', lexer_def.ECHO_E_DEF)
    TranslateSimpleLexer('MatchGlobToken', lexer_def.GLOB_DEF)
    TranslateSimpleLexer('MatchPS1Token', lexer_def.PS1_DEF)
    TranslateSimpleLexer('MatchHistoryToken', lexer_def.HISTORY_DEF)
    TranslateSimpleLexer('MatchBraceRangeToken', lexer_def.BRACE_RANGE_DEF)

    # e.g. "pipefail" -> option-I::pipefail
    pairs = [(opt.name, opt.index) for opt in option_def.All()]
    StringToInt('MatchOption', pairs)

    # e.g. "echo" -> builtin_i::echo
    pairs = [(b.name, b.index) for b in builtin_def.All()]
    StringToInt('MatchBuiltin', pairs)

    TranslateRegexToPredicate(lexer_def.VAR_NAME_RE, 'IsValidVarName')
    TranslateRegexToPredicate(pretty.PLAIN_WORD_RE, 'IsPlainWord')
    TranslateRegexToPredicate(lexer_def.SHOULD_HIJACK_RE, 'ShouldHijack')

  elif action == 'print-all':
    # Top level is a switch statement.
    for state, pat_list in lexer_def.LEXER_DEF.iteritems():
      print(state)
      # This level is re2c patterns.
      for is_regex, pat, token_id in pat_list:
        print('\t%r  ->  %r' % (pat, token_id))
        if is_regex:
          #print re_tree
          out_pat = TranslateRegex(pat)
          #print out_pat

      print()

  elif action == 'print-regex':
    unique = set()

    num_regexes = 0
    for state, pat_list in lexer_def.LEXER_DEF.iteritems():
      print(state)
      # This level is re2c patterns.
      for is_regex, pat, token_id in pat_list:
        #print '\t%r  ->  %r' % (pat, token_id)
        if is_regex:
          print('\t' + pat)
          print('\t' + TranslateRegex(pat))
          print()
          #PrintRegex(pat)
          num_regexes += 1
          unique.add(pat)
        else:
          print('\t' + TranslateConstant(pat))

      print()

    print('Printed %d regexes (%d unique)' % (num_regexes, len(unique)))