Jump to:  OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long)
Index:  All • Variables • Functions • Objects • Targets • Options

Chapter 16  Parsers

16.1  C Parser

The standard library file parse/C/Parse.om implements a parser for the C language. The following code fragment gives an example of a function to parse a file.

    open parse/C/Parse

    parse-file(filename) =
        prog = $(parser.parse-file prog, $(filename))
        ...

The value prog that is returned is derived from the object Prog 16.1.3, which splits the program into the following parts: 1) an array of definitions, 2) a table of struct definitions, 3) a table of enum definitions, and 4) a table of typedefs.

Each of the programs parts is defined through the following objects in the form of an abstract syntax tree (AST), with methods for performing some operations like resolving type definitions, printing out the tree, etc.

The AST is defined through the following classes, where we use the notation C/Parse::<object-name> to represent an object in the C AST.

16.1.1  C/Parse::TypeTable

Extends: Map 13.1.2

This object represents a table that maps type names to Type 16.1.68 values.

16.1.2  C/Parse::Base

The base class from which all parser objects are derived.

Fields:

Every parser item has a location, which is initialized to the value of parse-loc at the time the item is created.

Note: if you are defining new items manually, you must define parse-loc manually. For example, here is the definition of the method Type.reference().

    Type. =
        class Type
        ...
        reference() =
            parse-loc = $(this.loc)
            return $(TypePoiunter.make $(this))

16.1.3  C/Parse::Prog

Extends: Base 16.1.2.

The Prog object is used to represent the abstract syntax tree for a C program. The program has four parts.

16.1.4  C/Parse::Op

Extends: Base 16.1.2.

The Op object represents an operator.

Fields:

16.1.5  C/Parse::Unop

Extends: C/Parse::Op 16.1.4.

Unary operators.

16.1.6  C/Parse::Binop

Extends: C/Parse::Op 16.1.4.

16.1.7  C/Parse::Ternop

Extends: C/Parse::Op 16.1.4.

Ternary operators (in C, this is only exp ? exp : exp).

16.1.8  C/Parse::Exp

Extends: Base 16.1.2.

The base class for expressions.

16.1.9  C/Parse::LiteralExp

Extends: C/Parse::Exp 16.1.8.

A LiteralExp represents a constant.

Fields:

16.1.10  C/Parse::CharExp

Extends: C/Parse::LiteralExp 16.1.9.

A character constant. The field val is the constant with quotations, for example '\n'.

16.1.11  C/Parse::IntExp

Extends: C/Parse::LiteralExp 16.1.9.

An integer constant. The field val is the constant in source form, with any radix prefix and/or precision suffix, for example 0xabcdL.

16.1.12  C/Parse::FloatExp

Extends: C/Parse::LiteralExp 16.1.9.

An floating-pointer constant. The field val is the constant in source form, for example 31.415926e-1.

16.1.13  C/Parse::StringExp

Extends: C/Parse::LiteralExp 16.1.9.

A string constant. The field val is the constant in source form, with quotes, for example "Hello world\n".

16.1.14  C/Parse::IdExp

Extends: C/Parse::LiteralExp 16.1.9.

An identifier (a type or variable name). The field val is the name of the identifier.

16.1.15  C/Parse::Exp1

Extends: C/Parse::Exp 16.1.8.

This is an expression with an operator an one subexpression.

Fields:

16.1.16  C/Parse::PreExp1

Extends: C/Parse::Exp1 16.1.15.

A pre-operation, such as --i.

16.1.17  C/Parse::PostExp1

Extends: C/Parse::Exp1 16.1.15.

A post-operation, such as i--.

16.1.18  C/Parse::Exp2

Extends: C/Parse::Exp 16.1.8.

An expression with an operator and two subexpressions, for example, 1 - 2.

Fields:

16.1.19  C/Parse::AssignExp

Extends: C/Parse::Exp2 16.1.18.

This represents an assignment operation. The operator can be either a simple assignment x = 1, or involve computation x *= 2.

16.1.20  C/Parse::Exp3

Extends: C/Parse::Exp 16.1.8.

This represents an expression with two operators and three subexpressions. In plain C, there is only one expression of this form, exp ? exp : exp. The first operator is ? and the second is :.

Fields:

16.1.21  C/Parse::ParensExp

Extends: C/Parse::Exp 16.1.8.

A parenthesized expression.

Fields:

16.1.22  C/Parse::StmtExp

Extends: C/Parse::Exp 16.1.8.

(GCC-specific) A compound statement expression, for example ({ x = 1; y = 2; }).

Fields:

16.1.23  C/Parse::SubscriptExp

Extends: C/Parse::Exp 16.1.8.

A subscripting operation arg1[arg2].

Fields:

16.1.24  C/Parse::ApplyExp

Extends: C/Parse::Exp 16.1.8.

A function application f(arg1, ..., argN).

Fields:

16.1.25  C/Parse::CastExp

Extends: C/Parse::Exp 16.1.8.

A type cast (type) exp.

Fields:

16.1.26  C/Parse::SizeofExp

Extends: C/Parse::Exp 16.1.8.

A sizeof expression, for example sizeof(int) or sizeof(1 - 2).

Fields:

16.1.27  C/Parse::Initializer

Extends: Base 16.1.2.

An initial value.

16.1.28  C/Parse::InitExp

Extends: C/Parse::Initializer 16.1.27.

An expression initializer. In plain C, the expression must be constant, for example the expression 1 - 2 in int x = 1 - 2.

Fields:

16.1.29  C/Parse::InitArray

Extends: C/Parse::Initializer 16.1.27.

An array initializer, for example int x[] = { 1, 2, 3 };.

Fields:

16.1.30  C/Parse::InitField

Extends: C/Parse::Initializer 16.1.27.

A structure field initializer (GCC-specific). The following definition contains field initializers of the form <identifier> : <exp>.

    struct foo { int x, y; };
    struct foo z = { y: 1; x: 1 + 2; };

Fields:

16.1.31  C/Parse::Stmt

Extends: Base 16.1.2.

A statement.

16.1.32  C/Parse::EmptyStmt

Extends: C/Parse::Stmt 16.1.31.

An empty statement, ;.

16.1.33  C/Parse::ExpStmt

Extends: C/Parse::Stmt 16.1.31.

An expression statement, for example 1;.

Fields:

16.1.34  C/Parse::DefaultStmt

Extends: C/Parse::Stmt 16.1.31.

Labels and cases are modeled as statements. The DefaultStmt represents the syntax default:.

Fields:

16.1.35  C/Parse::CaseStmt

Extends: C/Parse::Stmt 16.1.31.

Labels and cases are modeled as statements. The CaseStmt represents the syntax case <exp>:.

Fields:

16.1.36  C/Parse::LabelStmt

Extends: C/Parse::Stmt 16.1.31.

Labels and cases are modeled as statements. The LabelStmt represents the syntax <label>:.

Fields:

16.1.37  C/Parse::GotoStmt

Extends: Stmt 16.1.31.

A goto <label>; statement.

Fields:

16.1.38  C/Parse::ContinueStmt

Extends: Stmt 16.1.31.

A statement continue;.

16.1.39  C/Parse::BreakStmt

Extends: Stmt 16.1.31.

A statement break;.

16.1.40  C/Parse::ReturnStmt

Extends: Stmt 16.1.31.

A return statement, for example return 1;.

Fields:

16.1.41  C/Parse::BlockStmt

Extends: Stmt 16.1.31.

A compound statement, for example { x = 1; y = 2; }.

Fields:

16.1.42  C/Parse::WhileStmt

Extends: Stmt 16.1.31.

A while statement while(test) body.

Fields:

16.1.43  C/Parse::DoStmt

Extends: Stmt 16.1.31.

A do statement do body while(exp).

Fields:

16.1.44  C/Parse::ForStmt

Extends: Stmt 16.1.31.

A for statement for(init; test; post) body. Any of the files may be empty.

Fields:

16.1.45  C/Parse::IfStmt

Extends: Stmt 16.1.31.

An if statement if(test) stmt1 [else stmt2];. Use $(defined stmt2) to determine if stmt2 is defined.

Fields:

16.1.46  C/Parse::SwitchStmt

Extends: Stmt 16.1.31.

A switch statement switch(exp) body. The body is a compound statement—a list of statements with the case labels inline.

Fields:

16.1.47  C/Parse::AsmArg

Extends: Base 16.1.2.

An argument to an assembly directive. Syntax: : mode (arg).

Fields:

16.1.48  C/Parse::AsmStmt

Extends: Stmt 16.1.31.

An assembly directive.

    asm (exp args);

Fields:

16.1.49  C/Parse::TypeModBase

Extends: Base 16.1.2.

A type modifier.

16.1.50  C/Parse::TypeModNamed

Extends: TypeModBase 16.1.49.

Fields:

There are three kinds of named type modifiers: TypeClass, TypeMod, and TypeQual.

16.1.51  C/Parse::TypeClass

Extends: TypeModNamed 16.1.50.

For plain C, one of the words auto, extern, inline, register, static, volatile, __volatile, __inline, __inline__.

16.1.52  C/Parse::TypeMod

Extends: TypeModNamed 16.1.50.

For plain C, one of the words long, short.

16.1.53  C/Parse::TypeQual

Extends: TypeModNamed 16.1.50.

For plain C, one of the words const, signed, unsigned, __const, __restrict, __signed.

16.1.54  C/Parse::Var

Extends: Base 16.1.2.

A variable.

Methods:

16.1.55  C/Parse::VarNone

Extends: Var 16.1.54.

A phony identifier, used as a placeholder in a parameter list.

16.1.56  C/Parse::VarId

Extends: Var 16.1.54.

A normal identifier.

Fields:

16.1.57  C/Parse::VarInit

Extends: Var 16.1.54.

A variable with an initializer.

Fields:

16.1.58  C/Parse::VarQualified

Extends: Var 16.1.54.

A qualified variable, for example const x.

Fields:

16.1.59  C/Parse::VarAttribute

Extends: Var 16.1.54.

A variable with the GCC __attribute__ extension.

Fields:

16.1.60  C/Parse::VarPointer

Extends: Var 16.1.54.

A pointer, for example *x.

Fields:

16.1.61  C/Parse::VarRef

Extends: Var 16.1.54.

A reference, for example &x.

Fields:

16.1.62  C/Parse::VarArray

Extends: Var 16.1.54.

A reference, for example x[exp].

Fields:

16.1.63  C/Parse::VarFunction

Extends: Var 16.1.54.

A named function var(param1, ..., param2).

Fields:

16.1.64  C/Parse::VarField

Extends: Var 16.1.54.

A variable with a field specifier, like var : bits.

Fields:

16.1.65  C/Parse::Decl

Extends: Base 16.1.2.

A declaration.

Methods:

16.1.66  C/Parse::ElideDecl

Extends: Decl 16.1.65.

The elision ....

16.1.67  C/Parse::VarDecl

Extends: Decl 16.1.65.

A type variable declaration, like int x (the type is int and the variable is x).

Fields:

16.1.68  C/Parse::Type

Extends: Base 16.1.2.

A type.

Methods:

16.1.69  C/Parse::TypeId

Extends: Type 16.1.68

A type name.

Fields:

16.1.70  C/Parse::TypeElide

Extends: Type 16.1.68

A fake type, for elisions ....

16.1.71  C/Parse::TypeStruct

Extends: Type 16.1.68

A verb+struct+ or union type. A struct type may be complete with fields, or the fields may be omitted.

Fields:

Methods:

16.1.72  C/Parse::EnumFieldDecl

Extends: Base 16.1.2.

A field in a enum type. The field has a name and an optional value.

Fields:

16.1.73  C/Parse::TypeEnum

Extends: Type 16.1.68

A enum definition. A TypeEnum is like a TypeStruct 16.1.71, but the field definitions are of type EnumFieldDecl 16.1.72.

16.1.74  C/Parse::Type1

Extends: Type 16.1.68.

A type with one subterm.

Fields:

16.1.75  C/Parse::TypePtr

Extends: Type 16.1.74.

A pointer type.

16.1.76  C/Parse::TypePointer

Extends: Type 16.1.75.

A pointer type *ty.

16.1.77  C/Parse::TypeRef

Extends: Type 16.1.75.

A reference type &ty.

16.1.78  C/Parse::TypeArray

Extends: Type 16.1.75.

An array type ty[exp].

Fields:

16.1.79  C/Parse::TypeAttr

Extends: Type 16.1.74.

A type with some attributes.

16.1.80  C/Parse::TypeVar

Extends: Type 16.1.79.

A type with a name (for named parameters like int *p).

Fields:

16.1.81  C/Parse::TypeQualified

Extends: Type 16.1.79.

A type with qualifiers (like static, long, etc.).

Fields:

16.1.82  C/Parse::TypeCore

Extends: TypeQualified 16.1.81.

A type with qualifiers (like static, long, etc.).

16.1.83  C/Parse::TypeAttribute

Extends: TypeAttr 16.1.79.

A type with a GCC __attribute_+.

16.1.84  C/Parse::TypeField

Extends: TypeAttr 16.1.79.

A type with a bit length. The type should be an integer type.

16.1.85  C/Parse::TypeInit

Extends: TypeAttr 16.1.79.

A type with an initializer.

16.1.86  C/Parse::TypeFun

Extends: Type 16.1.68.

A function type. This isn't a pointer type; normally C function types are defined as a TypePointer 16.1.76 to a TypeFun.

16.1.87  C/Parse::Definition

Extends: Base 16.1.2.

A variable definition.

16.1.88  C/Parse::VarNoneDef

Extends: Definition 16.1.87.

A simple definition without a variable. This rather silly case is for type-only definitions, like int;.

Fields:

16.1.89  C/Parse::VarDefCore

Extends: Definition 16.1.87. Extends: TypeVar 16.1.80.

A varable definition, like int x;.

Fields:

16.1.90  C/Parse::ParamDef

Extends: VarDefCore 16.1.89.

A parameter definition.

16.1.91  C/Parse::VarDef

Extends: VarDefCore 16.1.89.

A variable definition, like int x;.

16.1.92  C/Parse::TypeDef

Extends: VarDefCore 16.1.89.

A typedef, like typedef int x. Typedefs basically have the same information as a variable definition.

16.1.93  C/Parse::CallbackDef

Extends: Base 16.1.2.

A __dll_callback definition. Callbacks are used by the DLL generator to declare functions that are callback. Syntactically, a callback definition is like a function declaration, but it uses the __dll_callback keyword.

    __dll_callback int my_callback(int arg);

Fields:

16.1.94  C/Parse::FunDef

Extends: Definition 16.1.87.

A function definition with a body.

Fields:

16.1.95  C/Parse::CppItem

Extends: Definition 16.1.87.

A pre-processor directive. The parser interpretes these directives literally.

Fields:

16.1.96  C/Parse::Extension

Extends: Base 16.1.2.

A GCC extension, defined with __extension__ ....

Fields:

Jump to:  OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long)
Index:  All • Variables • Functions • Objects • Targets • Options