Implementation:Ggml org Llama cpp Jinja Parser Header
| Knowledge Sources | |
|---|---|
| Domains | Template_Engine, Parsing |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the public interface for the Jinja parser and its exception type.
Description
This header exposes `parse_from_tokens()` which takes a `lexer_result` and returns a `program` (AST). It defines `parser_exception` extending `std::runtime_error` with source position context formatting via `fmt_error_with_source`, enabling detailed error messages that point to the problematic location in the template source. The internal `parser` class is kept encapsulated in the .cpp file, exposing only what callers need through this clean public API boundary.
Usage
Include this header when you need to parse tokenized Jinja templates into an AST. This is the second stage of template processing, after lexing and before runtime execution.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/jinja/parser.h
- Lines: 1-21
Signature
namespace jinja {
program parse_from_tokens(const lexer_result & lexer_res);
struct parser_exception : public std::runtime_error {
parser_exception(const std::string & msg, const std::string & source, size_t pos);
};
} // namespace jinja
Import
#include "jinja/parser.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| lexer_res | const lexer_result & | Yes | Tokenized Jinja template containing the token vector and original source string |
Outputs
| Name | Type | Description |
|---|---|---|
| parse_from_tokens return | program | The abstract syntax tree (AST) representing the parsed template |
| parser_exception | exception | Thrown on parse errors with source position context pointing to the problematic location |
Usage Examples
#include "jinja/lexer.h"
#include "jinja/parser.h"
// Lex the template
jinja::lexer lex;
auto tokens = lex.tokenize("{% if user %}Hello {{ user }}{% endif %}");
// Parse tokens into AST
try {
auto program = jinja::parse_from_tokens(tokens);
// program is ready for runtime execution
} catch (const jinja::parser_exception & e) {
// Detailed error with source position
}