Expand description
ยง๐ ๏ธ Coq Parser Developer Guide
Coq support for the Oak language framework.
This guide is designed to help you quickly get started with developing and integrating oak-coq.
ยง๐ฆ Quick Start
ยงBasic Parsing Example
The following is a standard workflow for parsing a simple Coq theorem:
use oak_coq::{CoqParser, SourceText, CoqLanguage};
fn main() {
// 1. Prepare source code
let code = r#"
Theorem add_0_n : forall n : nat, 0 + n = n.
Proof.
intros n. reflexivity.
Qed.
"#;
let source = SourceText::new(code);
// 2. Initialize parser
let config = CoqLanguage::new();
let parser = CoqParser::new(&config);
// 3. Execute parsing
let result = parser.parse(&source);
// 4. Handle results
if result.is_success() {
println!("Parsing successful! AST node count: {}", result.node_count());
} else {
eprintln!("Errors found during parsing.");
}
}ยง๐ Core API Usage
ยง1. Syntax Tree Traversal
After a successful parse, you can use the built-in visitor pattern or manually traverse the Green/Red Tree to extract Coq constructs like theorem declarations, proof tactics, or Gallina terms.
ยง2. Incremental Parsing
No need to re-parse the entire script when small changes occur:
// Assuming you have an old parse result 'old_result' and new source text 'new_source'
let new_result = parser.reparse(&new_source, &old_result);ยง3. Diagnostics
oak-coq provides rich error contexts specifically tailored for Coq developers:
for diag in result.diagnostics() {
println!("[{}:{}] {}", diag.line, diag.column, diag.message);
}ยง๐๏ธ Architecture Overview
- Lexer: Tokenizes Coq source text into a stream of tokens, handling keywords, operators, identifiers, and complex notation delimiters.
- Parser: Syntax analyzer based on the Pratt parsing algorithm to handle Coqโs extensible syntax, vernacular commands, and tactic blocks.
- AST: A strongly-typed syntax abstraction layer designed for building high-performance Coq analysis tools and interactive proof environments.
ยง๐ Advanced Resources
Re-exportsยง
pub use crate::ast::CoqRoot;pub use crate::builder::CoqBuilder;pub use crate::language::CoqLanguage;pub use crate::lexer::CoqLexer;pub use crate::parser::CoqParser;pub use crate::parser::element_type::CoqElementType;pub use lexer::token_type::CoqTokenType;pub use crate::lsp::highlighter::CoqHighlighter;pub use crate::lsp::CoqLanguageService;pub use crate::lsp::formatter::CoqFormatter;pub use crate::mcp::serve_coq_mcp;