Token classification and representation for expression parsing in fpx
This module provides the lightweight but robust token infrastructure used by the fpx preprocessor when evaluating constant expressions in #if / #elif directives.
It defines:
tokens_enum)token derived type that carries both the lexical value and its semantic categoryThese types are used internally by evaluate_expression() (from fpx_token) to parse and compute #if DEBUG > 1 && defined(USE_MPI)-style conditions.
#if evaluation: | Enumerator | Value | Meaning |
|---|---|---|
unknown | -1 | Invalid / unrecognized token |
number | 0 | Integer or floating-point literal |
operator | 1 | ?:, +, -, *, /, ==, !=, &&, ||, !, >, <, etc.| | identifier | 2 | Macro name or function name (e.g. defined) | | parenthesis| 3 | ( or ) | | defined | 4 | Special keyword defined (treated specially)| Data Types | |
| type | token |
| Represents a single token in a parsed expression. Holds the string value of the token and its classified type. More... | |
Enumerations | |
| enum | { unknown = -1 , number = 0 , operator = 1 , identifier = 2 , parenthesis = 3 , defined = 4 } |
| Token kinds used in expression parsing. Enumeration defining the possible types of tokens recognized by the tokenizer. More... | |
| integer, parameter, public tokens_enum = kind(unknown) |
|
private |
|
private |
Detects whether a string starts a typeless constant (hex, octal, binary). Used to avoid treating them as identifiers during tokenization.
| [in] | str | Input string starting at current position |
| [out] | pos | Length of the typeless constant (0 if not typeless) |
Remarks
| subroutine, public tokenize | ( | character(*), intent(in) | expr, |
| type(token), dimension(:), intent(out), allocatable | tokens, | ||
| integer, intent(out) | ntokens ) |
Tokenizes a preprocessor expression into an array of token structures. Handles whitespace, multi-character operators (&&, ||, ==, etc.), the defined operator (with or without parentheses), numbers in various bases, identifiers, and parentheses.
| [in] | expr | Expression string to tokenize |
| [out] | tokens | Allocated array receiving the tokens |
| [out] | ntokens | Number of tokens produced |
Remarks