Data Structure and Dart Programming language problems

  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us
  • Home
  • Data Structure
    • Array and String
    • Linklist
  • Google Flutter
    • Google Flutter
    • DART
  • About Us

Sunday, August 6, 2017

Dart replacing the node in the abstract syntax tree

 Tech host     4:15 AM     abstract-syntax-tree, dart, dart-analyzer, flutter, google-flutter     No comments   

Dart replacing the node in the abstract syntax tree

Here is the solution for that.
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/token.dart';

String src = """
void main(){
String a ="hey";
String b ="there"; 
print(\$a);
}
""";

AstNode ast;

main() async {

  ast = parseCompilationUnit(src, parseFunctionBodies: true);
  print(ast.toString());
  Visitor v = new Visitor();
  ast.visitChildren(v);
  print(ast.toString());
}    

class Visitor extends SimpleAstVisitor {
  @override
  visitFunctionDeclaration(FunctionDeclaration node) {
    for (var cn in node.childEntities) {
      if (cn.runtimeType.toString() == "FunctionExpressionImpl") {
        Visitor v = new Visitor();
        cn.visitChildren(v);
      }
    }
  }

  @override
  visitBlockFunctionBody(BlockFunctionBody node) {
    Visitor v = new Visitor();
    node.visitChildren(v);
 }

  @override
   visitBlock(Block node) {
     for (var cn in node.childEntities) {
       if (cn.runtimeType.toString() == "ExpressionStatementImpl") {
         Visitor v = new Visitor();
         cn.visitChildren(v);
       }
     }
   }

  @override
  visitMethodInvocation(MethodInvocation node) {
  String result = "print(\$a\$b)";
  Token resultToken =
    new StringToken(TokenType.STRING, result, node.beginToken.offset);
  AstNode replacement = new SimpleStringLiteral(resultToken, result);
  node.parent.accept(new NodeReplacer(node, replacement));
 }
}
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Older Post Home

0 comments:

Post a Comment

Blog Archive

  • ▼  2017 (18)
    • ▼  August (2)
      • Dart replacing the node in the abstract syntax tree
      • Dart Programming
    • ►  March (10)
    • ►  February (6)
  • ►  2016 (1)
    • ►  December (1)
  • ►  2013 (1)
    • ►  August (1)

Popular Posts

Categories

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question

About Me

Tech host
I am a blogger interested in programming and coding challenges. I am interested in knowing about the new technologies and new tech products that are launched daily and to write about them.
View my complete profile

Label

  • about-scra
  • abstract-syntax-tree
  • array
  • array-interview-question
  • array-left-rotation
  • compare-two-linked-list
  • cracking-the-coding-interview
  • dart
  • dart-analyzer
  • flutter
  • frequently-asked-question
  • google-flutter
  • interview-question
  • java
  • left-rotation
  • linklist-interview-question
  • linklist-print-a-node
  • linklist-question
  • popular-linklist-question
  • popular-string-question
  • scra
  • scra-2018
  • string
  • string-interview-question
  • string-palindrome-question
Powered by Blogger.
Revoltify

Revoltify

Copyright © Data Structure and Dart Programming language problems | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates