This is a dart parser made in Pharo.
To install it, first create a BLMoose image.
Then, in a playground perform:
Metacello new
githubUser: 'Evref-BL' project: 'SmaccDart' commitish: 'main' path: 'src';
baseline: 'SmaccDart';
onConflict: [ :ex | ex useIncoming ];
onUpgrade: [ :ex | ex useIncoming ];
onDowngrade: [ :ex | ex useLoaded ];
load
To parse a string:
DartParser parse: 'class A {
final int myInt;
final String myString;
}'
From a file
DartParser parseFile: 'path/to/the/file.dart'
With the project dart-dep-resolver, we use the parser to look for dependencies between dart file (using import declaration) and create a Mermaid diagram of these dependencies. The following is an example created from the template of the helloWorld app in Flutter.
flowchart
widget_test.dart --> main.dart
main.dart --> material.dart
widget_test.dart --> material.dart
widget_test.dart --> flutter_test.dart
You will find the grammar used for the parser in the file grammar/dart.g
.
To reimport it, use:
definition := ('path\to\grammar\dart.g' asFileReference contents).
grammarCompiler := SmaCCGrammarCompiler new.
grammarCompiler codeGenerator defaultCategory: 'SmaCC_Dart'.
grammarCompiler
buildDefinition: definition;
compileInto: 'DartScanner' andParser: 'DartParser'.
You can modify the dart.g file to improve the parser.
Each time you modify the .g file, remember to regenerate before commiting
Please refer to the official book of SmaCC (or its archived version).