PetitParser 是一個用於 Dart 的動態解析器框架,讓開發者能以最少的努力建立高效、可重複使用的語法與解析器。它支援流暢的 API 來定義解析規則,並針對效能進行優化,非常適合處理結構化文字,例如設定檔、領域特定語言或資料格式。此框架具有高度模組化與可擴展性,可輕鬆組合自訂的解析邏輯。由於支援所有主要平台,PetitParser 適用於需要強大文字分析功能的客戶端與伺服器端應用程式。
適用情境
解析設定檔
實現領域特定語言
處理結構化資料格式
構建程式碼分析器
建立自定義 DSL
主要特色
流暢的語法定義 API
高效能解析
模組化且可組合的規則
跨平台相容性
對遞迴語法的支援
可擴充的外掛系統
適合對象
開發自定義解析器的開發者
處理結構化文本輸入的團隊
需要 DSL 支持的應用程式
需要高效文本處理的項目
使用建議
初學者的學習曲線更陡峭
需要理解解析概念
有限的內建錯誤恢復支援
最適合複雜的解析任務,而非簡單的字串操作
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
PetitParser for Dart
PetitParser makes writing parsers in Dart fast, enjoyable, and fully type-safe.
Instead of writing fragile regular expressions or configuring complex external code generators, PetitParser lets you build parsers directly in plain Dart code. You assemble small, specialized parsers like LEGO bricks to model everything from simple configuration values to full programming languages.
✨ Why PetitParser?
🧩 Composable: Combine simple parsers into rich grammars using familiar operators and methods.
🛡️ Type-Safe: Take advantage of modern Dart records, generics, and pattern matching without casting dynamic types.
⚡ Fast & Efficient: Optimized character predicates and dedicated fast-path parsing keep execution blazing fast.
🔍 Debuggable: Step through parsing in your IDE, inspect tokens, and visualize execution trees with built-in tools.
🛠️ Extensible: Transform raw text directly into strongly typed models, syntax trees, or domain objects on the fly.
🌐 Open Source & Battle-Tested: Highly tested, well-documented, and trusted across production Dart and Flutter applications.
🚀 Quick Start
Add PetitParser to your Dart or Flutter project:
dart pub add petitparser
Import the package, assemble your parser, and parse key-value pairs:
import'package:petitparser/petitparser.dart';
voidmain() {
// 1. Match a key of one or more letters:final key = letter().plus().flatten();
// 2. Match a numeric value and convert it directly into an integer:final value = digit().plus().flatten().map(int.parse);
// 3. Combine them: match key, discard '=', and match value:final entry = key.trim().skip(after: char('=')).then(value.trim());
// 4. Transform the parsed pair into a typed Dart record:final parser = entry.map2((k, v) => (key: k, value: v));
// 5. Parse the input:final result = parser.parse('port = 8080');
print(result.value); // (key: port, value: 8080)
}
Notice what happened here:
letter().plus() matched consecutive letters, and .flatten() turned them into a String.
digit().plus() matched consecutive digits, and .map(int.parse) transformed them immediately into an int.
.skip(after: char('=')) consumed the separator without cluttering the result.
.then(...) combined both parts into a strongly typed record (String, int).
.map2(...) mapped both positional values into a readable named record.
🧩 Core Building Blocks
Every grammar in PetitParser is constructed from three fundamental concepts: matching text, combining parsers, and transforming results.
🔡 Matching Text
Primitive parsers recognize basic character patterns:
char('a') matches an exact single character:
char('a').parse('a').value; // 'a'
string('dart') matches an entire string:
string('dart').parse('dart').value; // 'dart'
digit() matches any digit from 0 to 9:
digit().parse('7').value; // '7'
letter() matches any uppercase or lowercase letter:
letter().parse('x').value; // 'x'
word() matches a letter, digit, or underscore:
word().parse('_').value; // '_'
whitespace() matches spaces, tabs, and newlines:
whitespace().parse(' ').value; // ' '
pattern('0-9a-fA-F') matches any character within a range:
pattern('0-9a-fA-F').parse('f').value; // 'f'
any() matches any character:
any().parse('!').value; // '!'
Most character parsers accept optional parameters such as ignoreCase: true for case-insensitive matching and unicode: true for full Unicode code-point support.
🔗 Combining Parsers
Snap individual parsers together to recognize structured input:
Sequences: Run parsers in order. letter().then(digit()) returns a typed Dart record with each result:
Calling parse on a string returns a Result. You can inspect the outcome using Dart pattern matching:
final parser = digit().plus().flatten().map(int.parse);
voidhandleResult(Result<int> result) {
switch (result) {
caseSuccess(value: final value):
print('Parsed $value');
caseFailure(message: final message, position: final position):
print('Error at $position: $message');
}
}
handleResult(parser.parse('123')); // Parsed 123handleResult(parser.parse('abc')); // Error at 0: digit expected
If you only need to know whether an input string conforms to your grammar, use accept:
if (parser.accept('123')) {
print('Valid input');
}
To extract all matching occurrences across a larger body of text, use allMatches:
final words = letter().plus().flatten();
final matches = words.allMatches('two words 123');
print(matches); // ('two', 'words')
🛠️ Practical Examples
📋 Delimited Lists
This example parses a comma-separated list of integers like '1, 2, 3' into a Dart List<int>:
final number = digit().plus().flatten().map(int.parse);
final numbers = number
.plusSeparated(char(',').trim())
.map((list) => list.elements);
final result = numbers.parse('1, 2, 3');
print(result.value); // [1, 2, 3]
list.elements gives you a typed List<int> containing only the parsed numbers, neatly dropping the comma characters.
🧮 Expression Builder
This example parses and evaluates mathematical expressions containing numbers, parentheses, negation, exponentiation, multiplication, division, addition, and subtraction:
This example parses nested JSON-style arrays of numbers like '[1, [2, 3], 4]' into a hierarchy of Dart lists:
final value = undefined<Object>();
// An array contains zero or more values separated by commas:final array = char('[')
.trim()
.then(value.starSeparated(char(',').trim()))
.then(char(']').trim())
.map3((open, elements, close) => elements.elements);
final number = digit().plus().flatten().map(int.parse);
// Wire the recursive definition into the placeholder:
value.set([number, array].toChoiceParser());
final parser = value.end();
print(parser.parse('[1, [2, 3], 4]').value); // [1, [2, 3], 4]
🏛️ Large Grammars
When a grammar grows to dozens or hundreds of productions, organizing rules as separate variables can become difficult to maintain. Subclassing GrammarDefinition lets you organize rules as methods on a class, resolving complex mutual recursions cleanly.
This example parses and evaluates arithmetic expressions using a modular, strongly typed grammar definition:
You can also build a parser starting from any specific production rule in the grammar using buildFrom:
final numberParser = definition.buildFrom(ref0(definition.number));
print(numberParser.parse('42').value); // 42
Organizing grammars this way makes them easy to navigate, refactor, and test production by production in your IDE.
🔬 Advanced Capabilities
📍 Source Positions and Tokens
If you are writing a compiler, linter, or syntax highlighter, you often need to know exactly where in the source file an element appeared. Wrap any parser with .token() to capture its start and stop offsets, line number, and column number:
final id = letter().plus().flatten().token();
final token = id.parse('hello').value;
print(token.value); // 'hello'print(token.start); // 0print(token.stop); // 5print(token.line); // 1print(token.column); // 1
🔭 Lookaheads and Semantic Guards
You can inspect the upcoming input stream without consuming characters:
parser.and() performs positive lookahead, succeeding only if parser matches next without advancing the input position.
parser.not() performs negative lookahead, succeeding only if parser fails to match next.
parser.where(predicate) rejects a match dynamically if a condition is not met.
For example, match a keyword like let only when it is not immediately followed by more word characters:
final keyword = string('let').skip(after: word().not());
print(keyword.parse('let').value); // 'let'print(keyword.accept('letter')); // false
⏳ Lazy Repetition
Standard repetition combinators like star() are possessive and consume as much input as possible. When matching delimited content like multi-line comments or quoted strings, use starLazy or plusLazy to stop as soon as a boundary parser succeeds:
Grammars can contain subtle issues such as unreachable branches, infinite loops, or accidental left-recursions. PetitParser provides a built-in linter in package:petitparser/reflection.dart that you can run in your tests:
For performance tuning, import package:petitparser/debug.dart and use profile(parser) to measure invocation counts and execution time per parser, or progress(parser) to inspect backtracking behavior.
📚 Resources
Website: Interactive web demos and ports to other languages.
Examples Repository: Full implementations for JSON, Lisp, Math, Smalltalk, and more.
📜 History
Grammars for programming languages are traditionally specified statically. They are hard to compose and reuse due to ambiguities that inevitably arise. PetitParser combines ideas from scannerless parsing, parser combinators, parsing expression grammars (PEG), and packrat parsers to model grammars and parsers as objects that can be reconfigured dynamically.
PetitParser was originally created in Smalltalk. Later on, as a way to learn and explore these platforms, I reimplemented PetitParser in Java and Dart. All implementations share a consistent architecture while adopting the idioms and modern best practices of each target language.