Beispiel #1
0
def p_field_access_id(p):
    'field_access : ID'
    vname = p[1]
    v = current_vartable.find_in_scope(vname)
    if (v != None):
        # local variable in current scope
        p[0] = ast.VarExpr(v, p.lineno(1))
    else:
        c = ast.lookup(ast.classtable, vname)
        if (c != None):
            # there is a class with this name
            p[0] = ast.ClassReferenceExpr(c, p.lineno(1))
        else:
            # reference to a non-local var; assume field
            p[0] = ast.FieldAccessExpr(ast.ThisExpr(p.lineno(1)), vname, p.lineno(1))
Beispiel #2
0
num_expr = num.copy().setParseAction(lambda t: ast.ConstExpr(int(t[0])))

base27_expr = base27.copy().setParseAction(lambda t: ast.ConstExpr(t[0]))

fcall_args = Group(Optional(expr + ZeroOrMore(Suppress(',') + expr)))

fcall_expr = (identifier + '(' + fcall_args +
              ')').setParseAction(lambda t: ast.FCall(t[0], t[2]))

sizeof_expr = (Keyword('sizeof') +
               type_identifier).setParseAction(lambda t: ast.SizeOf(t[1]))

parens_expr = (Literal('(') + expr +
               Literal(')')).setParseAction(lambda t: t[1])

var_expr = identifier.copy().setParseAction(lambda t: ast.VarExpr(t[0]))

const_ref_expr = (":" +
                  identifier).setParseAction(lambda t: ast.ConstRefExpr(t[1]))

deref_expr = ("*" + expr).setParseAction(lambda t: ast.DerefExpr(t[1]))

string_expr = quotedString.copy().setParseAction(
    lambda t: ast.StringExpr(t[0][1:-1]))

# All expressions except for infix ones.
bottom_expr << MatchFirst([
    num_expr, struct_access, fcall_expr, sizeof_expr, var_expr, const_ref_expr,
    base27_expr, parens_expr, deref_expr, string_expr, special_loc
])