Plasma GitLab Archive
Projects Blog Knowledge


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:

  • loc : Location 13.1.18 the location of the item.

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.

  • defs : Definition Array 16.1.87 is the list of declarations and definitions in the program.
  • typedefs : TypeTable 16.1.1 is a table that maps type names to their definitions.
  • structs : TypeTable 16.1.1 is a table of structure definitions.
  • tagged-unions : TypeTable 16.1.1 is a table that maps “tagged” unions to their definitions. A tagged union is a C union in which one integer field, the “tag”, specifies the variant.
  • enums : TypeTable 16.1.1 is a table that maps enumeration names to their definitions.

16.1.4  C/Parse::Op

Extends: Base 16.1.2.

The Op object represents an operator.

Fields:

  • op : String is the name of the operator

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:

  • val : String the string representation of the constant.

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:

  • op : Unop 16.1.5 the operator.
  • arg : Exp 16.1.8 the subexpression.

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:

  • op : Binop 16.1.6 the operator.
  • arg1, arg2 : Exp 16.1.8 the subexpressions.

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:

  • op1, op2 : Ternop 16.1.7.
  • arg1, arg2, arg3 : Exp 16.1.8.

16.1.21  C/Parse::ParensExp

Extends: C/Parse::Exp 16.1.8.

A parenthesized expression.

Fields:

  • exp : Exp 16.1.8 the subexpression.

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:

  • stmt : Exp 16.1.8 the statement, represented as an expression.

16.1.23  C/Parse::SubscriptExp

Extends: C/Parse::Exp 16.1.8.

A subscripting operation arg1[arg2].

Fields:

  • arg1, arg2 : Exp 16.1.8 the subexpressions.

16.1.24  C/Parse::ApplyExp

Extends: C/Parse::Exp 16.1.8.

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

Fields:

  • var : String the function.
  • args : Exp Array 16.1.8 the arguments to the function.

16.1.25  C/Parse::CastExp

Extends: C/Parse::Exp 16.1.8.

A type cast (type) exp.

Fields:

  • type : Type 16.1.68 the type.
  • exp : Exp 16.1.8 the expression to be cast.

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:

  • exp : Exp 16.1.8 the expression.

16.1.29  C/Parse::InitArray

Extends: C/Parse::Initializer 16.1.27.

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

Fields:

  • exp_list : Initializer Array 16.1.27 the list of initial values.

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:

  • name : String the identifier.
  • exp : Initializer 16.1.27 the initializer.

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:

  • exp : Exp 16.1.8 the expression

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:

  • exp : Exp 16.1.8 the expression

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:

  • exp : Exp 16.1.8 the expression.

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:

  • name : String the label.

16.1.37  C/Parse::GotoStmt

Extends: Stmt 16.1.31.

A goto <label>; statement.

Fields:

  • name : String the label.

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:

  • exp : Exp 16.1.8 the expression to return. If the statement has no return value return;, then exp is an EmptyStmt 16.1.32.

16.1.41  C/Parse::BlockStmt

Extends: Stmt 16.1.31.

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

Fields:

  • stmts : Stmt Array 16.1.31 the list of statements in the block.

16.1.42  C/Parse::WhileStmt

Extends: Stmt 16.1.31.

A while statement while(test) body.

Fields:

  • test : Exp 16.1.8 the loop test.
  • body : Stmt 16.1.31 the loop body.

16.1.43  C/Parse::DoStmt

Extends: Stmt 16.1.31.

A do statement do body while(exp).

Fields:

  • body : Stmt 16.1.31 the loop body.
  • exp : Exp 16.1.8 the loop test.

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:

  • init, test, post : Exp 16.1.8.
  • body : Stmt 16.1.31 the loop body.

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:

  • test: Exp 16.1.8.
  • stmt1, stmt2 : Stmt 16.1.31 the conditional bodies.

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:

  • mode : String.
  • arg : Exp 16.1.8.

16.1.48  C/Parse::AsmStmt

Extends: Stmt 16.1.31.

An assembly directive.

    asm (exp args);

Fields:

  • id : String the keyword, usually either asm or __asm__.
  • exp : Exp 16.1.8 the assembly expression.
  • args : AsmArg Array 16.1.47 the assembly arguments.

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:

  • name : String the modifier.

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:

  • is-core(): this variable is a simple identifier.
  • is-none(): this is a null variable (used in parameter declarations).
  • to-fun(): translate the variable to a type.
  • to-string() : String the string representation.
  • to-identifier() : String the identifier name.
  • to-id() : String the identifier name.
  • to-var() : Var 16.1.54 translate to a variable.
  • replace-var(x : String) : VarId replace the identifier with a new name x.
  • replace-fun(x : String) : VarId replace the identifier with a new name x.
  • to-type(ty : Type) : Type 16.1.68 produce a type from the declaration.

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:

  • id : String the identifier.

16.1.57  C/Parse::VarInit

Extends: Var 16.1.54.

A variable with an initializer.

Fields:

  • var : Var 16.1.54 the variable.
  • exp : Exp 16.1.8 the initializer.

16.1.58  C/Parse::VarQualified

Extends: Var 16.1.54.

A qualified variable, for example const x.

Fields:

  • var : Var 16.1.54 the variable.
  • qualifiers : TypeMod Array the qualifiers.

16.1.59  C/Parse::VarAttribute

Extends: Var 16.1.54.

A variable with the GCC __attribute__ extension.

Fields:

  • var : Var 16.1.54 the variable.
  • attribute : Exp 16.1.8 the attribute.

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:

  • var : Var 16.1.54 the variable.
  • exp : Exp 16.1.8 the dimension.

16.1.63  C/Parse::VarFunction

Extends: Var 16.1.54.

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

Fields:

  • var : Var 16.1.54 the function.
  • params : Definition Array 16.1.87 the parameters.

16.1.64  C/Parse::VarField

Extends: Var 16.1.54.

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

Fields:

  • var : Var 16.1.54 the variable.
  • bits : Exp 16.1.8 the bit-field width.

16.1.65  C/Parse::Decl

Extends: Base 16.1.2.

A declaration.

Methods:

  • is-elide() : Bool the declaration is an elision (...).
  • to-string() : String the string representation of the declaration.
  • to-identifier() : String the identifier associated with the declaration.
  • to-var() : Var the variable associated with the declaration.
  • replace-var(v) : Decl replace the variable with a new one v.
  • to-type() : Type get the type for the declaration.

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:

  • type : Type 16.1.68 the type of the declaration.
  • var : Var 16.1.54 the variable being declared.

16.1.68  C/Parse::Type

Extends: Base 16.1.2.

A type.

Methods:

  • is-void() : Bool is this the void type?
  • is-scalar() : Bool is the type a scalar type (a number character, or pointer)?
  • is-struct-or-enum() : Bool is the type a struct or enum type.
  • storage-info() : String one of the following values:
    • false the type is a char or numeric type.
    • nonscalar the type is not a scalar type.
    • pointer the type is a pointer.
    • ref the type is a reference type.
  • no-fields() : Type 16.1.68 remove any fields from a struct, union, or enum type.
  • unqualified() : Type 16.1.68 remove any type qualifiers.
  • to-extern() : Type 16.1.68 remove type qualifiers that are not useful for extern declarations.
  • to-pointer() : Type 16.1.68 convert reference and array types to pointer types.
  • dereference() : Type 16.1.68 dereference a pointer type (remove one level of indirection).
  • reference() : Type 16.1.68 add a level of indirection.
  • resolve() : Type 16.1.68 resolve type names to get complete type definitions.
  • simplify() : Type 16.1.68 resolve the type definition and convert any outermost array or reference to a pointer type (useful for parameters).
  • to-string() : String 16.1.68 produce the string representation of the type.
  • to-ml-string() : String 16.1.68 produce an string representation of the type in OCaml form.
  • to-name() : String 16.1.68 get a canonical name for the type if it has one.

16.1.69  C/Parse::TypeId

Extends: Type 16.1.68

A type name.

Fields:

  • id : String the type name.

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:

  • kind : String one of struct or union.
  • name : String the name of the struct. In anonymous structs, as in struct { int x; } foo;, the name will be fabricated.
  • fields : VarDecl Array 16.1.67 an optional list of field declarations.

Methods:

  • is-struct-or-enum() : Bool returns true.
  • no-fields() : TypeStruct 16.1.71 returns the type without any field declarations.
  • no-subfields() : TypeStruct 16.1.71 returns the type without any nested field definitions.
  • to-name() : String get a canonical type name for the struct.
  • resolve() : TypeStruct 16.1.71 if the fields are defined, resolve their definitions.
  • find-fields() : TypeStruct 16.1.71 resolve and field definitions (from typedefs) and return a complete definition if possible.

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:

  • name : String the name of the constant.
  • val : Exp option 16.1.8 the value of the field, if there is an explicit value.

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:

  • exp : Exp 16.1.8 the dimension of the array.

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:

  • qualifiers : TypeMod Array 16.1.52 the qualifiers.

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_+.

  • attribute : Exp 16.1.8 the 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.

  • bits : Exp 16.1.8 the bit-field length.

16.1.85  C/Parse::TypeInit

Extends: TypeAttr 16.1.79.

A type with an initializer.

  • exp : Exp 16.1.8 the initial value.

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.

  • ty : Type 16.1.68 the return type.
  • params : Type Array 16.1.68 the types of the parameters.

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:

  • def : Definition 16.1.87 the definition.

16.1.94  C/Parse::FunDef

Extends: Definition 16.1.87.

A function definition with a body.

Fields:

  • def : Definition 16.1.87 the definition (without the body).
  • body : Stmt 16.1.31 the body.

16.1.95  C/Parse::CppItem

Extends: Definition 16.1.87.

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

Fields:

  • line : String the directive (uninterpreted).

16.1.96  C/Parse::Extension

Extends: Base 16.1.2.

A GCC extension, defined with __extension__ ....

Fields:

  • item : Exp 16.1.8 the extension. The item is usually an Exp, but for forward compatibility it may be a value of any type.
Jump to:  OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long)
Index:  All • Variables • Functions • Objects • Targets • Options
This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml