1 parser
liquibyte edited this page 2026-11-19 16:23:29 -06:00

parser.h

The C Grammar Check, and the C++ Reader.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

The parser reads the tokens that come out of the preprocessor. Every token index in this header is an index into that PreprocessorToken array, not into the file's own tokens; PreprocessorToken.spelled leads back to the file.

Features:

  • parse_c(): whether the tokens form a C translation unit, C23 with the GNU extensions (statement expressions, nested functions, case ranges, computed goto, attribute, asm, typeof, __builtin_va_arg and the like, and K&R definitions), with checks the grammar alone misses: break, continue, case and default outside a loop or switch, an object declared twice in one block (not a typedef, an extern or a function), a function declared static in a block, a call with the wrong number of arguments (to a function the file declares with a prototype that is not variadic), and a name the file declares only inside blocks used outside them
  • Where the tokens are not one, the token where reading stopped, and why
  • parse_c_record(): also what it read, every declaration (its kind, scope and extent), every use of a name resolved to its declaration, and every scope. After a declaration that does not parse it goes on with the next, so one broken function hides only itself. The semantic model is built from this record
  • parse_cpp_record(): the same record for C++, without a grammar check

Internal design notes:

  • Recursive descent that backtracks where C is ambiguous without knowing every type: a name nothing declared may be a type or an object, and the text is accepted if either reading parses. The typedefs it meets are kept by scope, so a known name is not ambiguous
  • It checks the grammar: types are the semantic model's work
#include "parser.h"

Types: ParseResult, ParseKind, ParseUseKind, ParseDeclaration, ParseUse, ParseScope, ParseRecord

Constants: PARSE_NONE

Functions: parse_c, parse_c_record, parse_cpp_record, parse_record_free

Types

ParseResult

How a parse ended.

Field Type Description
valid int 1 when the grammar accepts all of it (for C++: when its brackets pair)
stopped_at size_t the token where reading stopped; the token count when it is valid
message[128] char what was wrong there, for people ("not C at \"else\""); "" when it is valid

ParseKind

What a declaration declares.

Name Value Description
PARSE_VARIABLE a variable
PARSE_FUNCTION a function
PARSE_PARAMETER a parameter of a function
PARSE_TYPEDEF a typedef name (in C++ also a using alias, a namespace alias, a concept or a template's type parameter)
PARSE_TAG the name of a struct, union or enum (in C++ also a class or namespace)
PARSE_MEMBER a member of a struct or union
PARSE_ENUMERATOR a constant of an enum
PARSE_LABEL a label for goto

ParseUseKind

How a name is used, which says where its declaration is looked for.

Name Value Description
PARSE_USE_NAME an object, function, typedef or enumerator: found through the scopes
PARSE_USE_TAG a name after struct, union or enum
PARSE_USE_MEMBER a name after . or ->
PARSE_USE_LABEL a name after goto, or after && (a label's address)

ParseDeclaration

One declaration the parser read.

Field Type Description
name uint32_t the token of its name
first uint32_t the declaration's first token (its specifiers)
end uint32_t one past its last token (the end of its declarator): its tokens are [first, end)
kind uint8_t what it declares: a ParseKind
is_definition uint8_t 1 for a function with its body, a tag with its body, and every parameter, member, enumerator and label (the semantic model decides a variable's)
scope int32_t the scope it belongs to: an index into ParseRecord.scopes (scope 0 is the file's)
owner int32_t C++: the class, namespace or enum it belongs to (a declaration's index); -1 for none, and always -1 in C.
body uint32_t C++: the { of the class, namespace or enum it declares; PARSE_NONE for none, and always in C.

ParseUse

One use of a name, and what it resolves to.

Field Type Description
token uint32_t the token of the name
kind uint8_t how it is used: a ParseUseKind
declaration int32_t the declaration it resolves to (an index into ParseRecord.declarations); -1 when the parser did not resolve it: a name that nothing in scope declares, a member, a label, and (in C) every tag, which the semantic model resolves

ParseScope

One scope: a block, or a parameter list.

Field Type Description
parent int32_t the scope around it; -1 for the file's own scope
first uint32_t where the parser opened it: the token after a block's { or a parameter list's (, or a function body's {
last uint32_t where the parser closed it: a block's }, a parameter list's ), or the token after a function body's }

ParseRecord

What the parser read: declarations, uses and scopes, by token index.

The arrays belong to the record; free them with parse_record_free().

Field Type Description
declarations ParseDeclaration * every declaration, in the file's order
declaration_count size_t how many declarations there are
declaration_capacity size_t room allocated for them (the parser's own)
uses ParseUse * every use of a name, in the file's order
use_count size_t how many uses there are
use_capacity size_t room allocated for them (the parser's own)
scopes ParseScope * every scope; scope 0 is the file's
scope_count size_t how many scopes there are
scope_capacity size_t room allocated for them (the parser's own)

Constants

PARSE_NONE

#define PARSE_NONE UINT32_MAX

No token: the value of a field that names none.

Functions

parse_c

int parse_c(const PreprocessorToken *tokens, size_t count, ParseResult *result);

Check whether preprocessed tokens are a valid C translation unit.

It stops at the first declaration that does not parse.

Parameter Description
tokens The tokens, as preprocess() gives them
count How many tokens there are
result Filled in

Returns: 0, or -1 when out of memory

parse_c_record

int parse_c_record(
    const PreprocessorToken *tokens,
    size_t count,
    ParseResult *result,
    ParseRecord *record);

parse_c(), keeping what it read

After a declaration that does not parse, it skips to the next one and goes on, so the record covers the rest of the file; result tells the first fault.

Parameter Description
tokens The tokens, as preprocess() gives them
count How many tokens there are
result Filled in
record Filled in; free it with parse_record_free()

Returns: 0, or -1 when out of memory

parse_cpp_record

int parse_cpp_record(
    const PreprocessorToken *tokens,
    size_t count,
    ParseResult *result,
    ParseRecord *record);

Read what C++ tokens declare and use, into a record.

There is no grammar check, because C++ cannot be parsed without knowing every type, and an editor needs the names of code that does not compile yet: each statement is read by its form, and result->valid says only whether the brackets pair. Each declaration also gets its owner and body. A member after . or -> is left unresolved, for the semantic model.

Parameter Description
tokens The tokens, as preprocess() gives them for a C++ file
count How many tokens there are
result Filled in
record Filled in; free it with parse_record_free()

Returns: 0, or -1 when out of memory

parse_record_free

void parse_record_free(ParseRecord *record);

Free what a record holds, and clear it.

Parameter Description
record The record, or NULL (nothing is done)