Skip to content

Commit

Permalink
Update to latest PetitParser.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Aug 13, 2023
1 parent dcbef66 commit e510de8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
8 changes: 5 additions & 3 deletions lib/src/xml/utils/character_data_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class XmlCharacterDataParser extends Parser<String> {
? buffer.indexOf(_stopper, position)
: buffer.length;
final end = index == -1 ? buffer.length : index;
return end - position < _minLength
? context.failure('Unable to parse character data.')
: context.success(buffer.substring(position, end), end);
if (end - position < _minLength) {
return context.failure('Unable to parse character data.');
} else {
return context.success(buffer.substring(position, end), end);
}
}

@override
Expand Down
8 changes: 4 additions & 4 deletions lib/src/xml_events/converters/event_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class _XmlEventDecoderSink extends StringConversionSinkBase {
}
final result = <XmlEvent>[];
Result<XmlEvent> previous =
Failure<XmlEvent>(carry + str.substring(start, end), 0, '');
Failure(carry + str.substring(start, end), 0, '');
for (;;) {
final current = eventParser.parseOn(previous);
if (current.isSuccess) {
if (current is Success) {
final event = current.value;
annotator.annotate(
event,
Expand All @@ -119,8 +119,8 @@ class _XmlEventDecoderSink extends StringConversionSinkBase {
@override
void close() {
if (carry.isNotEmpty) {
final context = eventParser.parseOn(Failure<XmlEvent>(carry, 0, ''));
if (context.isFailure) {
final context = eventParser.parseOn(Failure(carry, 0, ''));
if (context is Failure) {
throw XmlParserException(context.message,
position: offset + context.position);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/xml_events/iterator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:petitparser/petitparser.dart' show Parser, Result, Failure;
import 'package:petitparser/core.dart';

import '../xml/entities/entity_mapping.dart';
import '../xml/exceptions/parser_exception.dart';
Expand All @@ -10,7 +10,7 @@ class XmlEventIterator implements Iterator<XmlEvent> {
XmlEventIterator(
String input, XmlEntityMapping entityMapping, this._annotator)
: _eventParser = eventParserCache[entityMapping],
_context = Failure<XmlEvent>(input, 0, '');
_context = Failure(input, 0, '');

final Parser<XmlEvent> _eventParser;
final XmlAnnotator _annotator;
Expand All @@ -26,7 +26,7 @@ class XmlEventIterator implements Iterator<XmlEvent> {
final context = _context;
if (context != null) {
final result = _eventParser.parseOn(context);
if (result.isSuccess) {
if (result is Success) {
_context = result;
_current = result.value;
_annotator.annotate(
Expand Down
3 changes: 2 additions & 1 deletion lib/xpath.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:meta/meta.dart';
import 'package:petitparser/core.dart';

import 'src/xml/nodes/node.dart';
import 'src/xml/utils/cache.dart';
Expand All @@ -17,7 +18,7 @@ extension XPathExtension on XmlNode {
final _parser = const XPathParser().build();
final _cache = XmlCache<String, Resolver>((expression) {
final result = _parser.parse(expression);
if (result.isFailure) {
if (result is Failure) {
throw XPathParserException(result.message,
buffer: expression, position: result.position);
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ homepage: https://github.com/renggli/dart-xml
environment:
sdk: ^3.0.0
dependencies:
collection: ^1.17.0
collection: ^1.18.0
meta: ^1.9.0
petitparser: ^6.0.0
dev_dependencies:
args: ^2.4.0
lints: ^2.0.0
lints: ^2.1.0
test: ^1.24.0

dependency_overrides:
Expand Down
13 changes: 7 additions & 6 deletions test/entity_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:petitparser/core.dart';
import 'package:petitparser/matcher.dart';
import 'package:test/test.dart';
import 'package:xml/src/xml/utils/character_data_parser.dart';
Expand Down Expand Up @@ -226,31 +227,31 @@ void main() {
final parser = XmlCharacterDataParser('*', 1);
test('parse without stopper', () {
final result1 = parser.parse('');
expect(result1.isFailure, isTrue);
expect(result1 is Failure, isTrue);
expect(result1.position, 0);

final result2 = parser.parse('a');
expect(result2.isSuccess, isTrue);
expect(result2 is Success, isTrue);
expect(result2.position, 1);
expect(result2.value, 'a');

final result3 = parser.parse('ab');
expect(result3.isSuccess, isTrue);
expect(result3 is Success, isTrue);
expect(result3.position, 2);
expect(result3.value, 'ab');
});
test('parse with stopper', () {
final result1 = parser.parse('*');
expect(result1.isFailure, isTrue);
expect(result1 is Failure, isTrue);
expect(result1.position, 0);

final result2 = parser.parse('a*');
expect(result2.isSuccess, isTrue);
expect(result2 is Success, isTrue);
expect(result2.position, 1);
expect(result2.value, 'a');

final result3 = parser.parse('ab*');
expect(result3.isSuccess, isTrue);
expect(result3 is Success, isTrue);
expect(result3.position, 2);
expect(result3.value, 'ab');
});
Expand Down

0 comments on commit e510de8

Please sign in to comment.