string_scanner
1.4.1, released 2 years ago by tools.dart.dev
A class for parsing strings using a sequence of patterns.
WASM-readyQuiet but busy
What it does
Exposes a StringScanner type that makes it easy to parse a string using a series of Patterns. It allows you to move scan pointers, match expected patterns, and handle errors during string parsing.
AI-written summary of version 1.4.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to build a custom string parser using patterns or regular expressions.
- You need detailed position tracking and error formatting while parsing text inputs.
Skip it when
- You are parsing heavily structured formats like XML or YAML where dedicated parsers already exist.
Strengths
- Simplifies sequential string parsing with patterns.
- Provides helpful methods for expectations and matching errors.
Weaknesses
- Requires manual imperative parsing logic for complex grammars.
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 1
import 'dart:math' as math;
import 'package:string_scanner/string_scanner.dart';
num parseNumber(String source) {
final scanner = StringScanner(source);
final negative = scanner.scan('-');
scanner.expect(RegExp(r'\d+'));
var number = num.parse(scanner.lastMatch![0]!);
scanner.expectDone();
return (negative ? -1 : 1) * number;
}Alternatives
Packages that do the same job and could replace string_scanner.
- petitparser
If you prefer a combinator-based parsing library over imperative scanning.
93Compare - xml
If you specifically need to parse XML documents.
92Compare
Works with
Packages commonly used together with string_scanner: ones it builds on, ones built on it, and its own family.
- source_spanstring_scanner depends on itStandard representation for source code locations, spans, and human-friendly error messages.
- analyzerSame familyPerforms static analysis of Dart code for tool integration and embedding.
- dart_styleSame familyProvides an API and CLI to format Dart source code automatically.
- source_mapsSame familyA Dart library to parse, build, and manipulate source map v3 files.
- htmlSame familyA Dart implementation of an HTML5 parser for strings and bytes.
- csslibSame familyA Dart library that parses CSS strings or byte lists into an AST.
- yaml_editSame familyYAML manipulation library preserving comments and whitespace
- source_genSame familySource code generation builders and utilities for the Dart build system.