gpt4 book ai didi

dart - 如何使用 Dart 分析器从源代码生成 AST 并使用 AST?

转载 作者:行者123 更新时间:2023-12-04 11:21:37 52 4
gpt4 key购买 nike

AST

文档:https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/parseCompilationUnit.html

一些聪明的程序员在野外的优秀示例:https://github.com/mythz/dart-samples/blob/master/frog/leg/scanner/keyword.dart

递归ASTVisitor api:https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/RecursiveASTVisitor-class.html

现在示例代码已移至下面的 wiki。

dart 分析服务器或 dart 服务

IDE 或编辑器用于检测错误的包。

混帐:https://github.com/dart-lang/dart-services

安装:

  • git 克隆 https://github.com/dart-lang/dart-services.git

  • 运行服务器:
  • cd Dart 服务
  • dart ./bin/services.dart --port 8082

  • 客户端代码:
    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);
    }

    服务器将/lib/src/common_server.dart 中的类 CommonServer 作为 api 提供服务。 “api/dartservices/v1/analyze”的帖子在类中调用以下方法。
      @ApiMethod(
    method: 'POST',
    path: 'analyze',
    description:
    'Analyze the given Dart source code and return any resulting '
    'analysis errors or warnings.')
    Future<AnalysisResults> analyze(SourceRequest request) {
    return _analyze(request.source);
    }

    使用 Analyzer 作为本地对象。
    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);
    }

    分析器相当复杂并且被微任务所笼罩,调试器并没有太大帮助。

    最佳答案

    dart 2.11,来自测试代码

    import 'package:analyzer/dart/analysis/utilities.dart';
    import 'package:analyzer/dart/analysis/results.dart';
    import 'package:test/test.dart';

    void main() {
    test('hello_parser', () {
    String content = '''
    void main() => print('Hello, world!')
    ''';
    ParseStringResult result = parseString(content: content, throwIfDiagnostics: false);
    // print(result.unit.toSource());
    expect(result.content, content);
    expect(result.errors, hasLength(1));
    expect(result.lineInfo, isNotNull);
    expect(result.unit.toString(), equals("void main() => print('Hello, world!');"));
    });
    }

    关于dart - 如何使用 Dart 分析器从源代码生成 AST 并使用 AST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534808/

    52 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com