blob: 1f4d38bed8ed4239fc37a4a04c5919384bac9a94 [file] [log] [blame]
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/string_source.dart';
import 'package:analyzer/src/dart/scanner/reader.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:meta/meta.dart';
class _ErrorListener implements AnalysisErrorListener {
final errors = <AnalysisError>[];
void onError(AnalysisError error) {
void throwIfErrors() {
if (errors.isNotEmpty) {
throw Exception(errors);
class CompilationUnitParser {
CompilationUnit parse({@required String contents, @required String name}) {
var reader = CharSequenceReader(contents);
var stringSource = StringSource(contents, name);
var errorListener = _ErrorListener();
var scanner = Scanner(stringSource, reader, errorListener);
var startToken = scanner.tokenize();
var parser = Parser(stringSource, errorListener, featureSet: null);
var cu = parser.parseCompilationUnit(startToken);
return cu;