How to use Dart analyzer to generate an AST from source and work with the AST?
<h2>AST</h2>

<p>Documentation:<a href="https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/parseCompilationUnit.html" rel="noreferrer">https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/parseCompilationUnit.html</a></p>

<p>An excellent sample by some smart programmer in the wild: <a href="https://github.com/mythz/dart-samples/blob/master/frog/leg/scanner/keyword.dart" rel="noreferrer">https://github.com/mythz/dart-samples/blob/master/frog/leg/scanner/keyword.dart</a></p>

<p>RecursiveASTVisitor api:<a href="https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/RecursiveASTVisitor-class.html" rel="noreferrer">https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/RecursiveASTVisitor-class.html</a></p>

<p>Now the sample code has been moved to the wiki below.</p>

<h2>dart analysis server or dart services</h2>

<p>The package your IDE or editor depends on for detecting errors.  </p>

<p>git: <a href="https://github.com/dart-lang/dart-services" rel="noreferrer">https://github.com/dart-lang/dart-services</a>  </p>

<p>Install:  </p>

<ul>
<li>git clone <a href="https://github.com/dart-lang/dart-services.git" rel="noreferrer">https://github.com/dart-lang/dart-services.git</a>  </li>
</ul>

<p>Run server:</p>

<ul>
<li>cd dart-services    </li>
<li>dart ./bin/services.dart --port 8082</li>
</ul>

<p>Client side code:</p>

<pre><code>import "dart:convert";
import "dart:io";


const String url = "localhost";
const String path = r"api/dartservices/v1/analyze";
String src = """
main(){
 print('hi');
}
""";

void main() {
  analyse();
}

analyse() async {
  Map m = {'source': src};
  var client = new HttpClient();
  String data = JSON.encode(m);
  HttpClientRequest request = await client.post (
      url, 8082, path);
  request.headers.contentType = ContentType.JSON;
  request.write(data);
  HttpClientResponse r = await request.close();
  r.transform(UTF8.decoder).listen((d){
    print(d);
  });
  client.close(force: false);
}
</code></pre>

<p>The server serves the class CommonServer in /lib/src/common_server.dart as api. A post to "api/dartservices/v1/analyze" calls the method below in the class.</p>

<pre><code>  @ApiMethod(
      method: 'POST',
      path: 'analyze',
      description:
          'Analyze the given Dart source code and return any resulting '
          'analysis errors or warnings.')
  Future&lt;AnalysisResults&gt; analyze(SourceRequest request) {
    return _analyze(request.source);
  }
</code></pre>

<h2>Using Analyzer as a local object.</h2>

<pre><code>import 'src/analyzer.dart';
import 'src/api_classes.dart';
import 'dart:developer';
String src = """
main(){
  a.print('hi');
}
""";
main() async {
  String sdkPath = r'C:\tools\dart-sdk';
  var a = new Analyzer(sdkPath);
  var m = {'main.dart':src};
  debugger();
  AnalysisResults r = await a.analyzeMulti(m);
  print(r.issues);
  print(r.resolvedImports);
}
</code></pre>

<p>Analyzer is fairly complicated and shrouded in micro-tasks, debugger was not much of a help.</p>