logo
class

compiler::AstWriter

sys::Obj
  compiler::AstWriter
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   15 Sep 05  Brian Frank  Creation
   7  //   24 Jun 06  Brian Frank  Ported from Java to Fan
   8  //
   9  
  10  **
  11  ** AstWriter
  12  **
  13  class AstWriter
  14  {
  15  
  16  //////////////////////////////////////////////////////////////////////////
  17  // Construction
  18  //////////////////////////////////////////////////////////////////////////
  19  
  20    **
  21    ** Make for specified output stream
  22    **
  23    new make(OutStream out := Sys.out)
  24    {
  25      this.out = out
  26    }
  27  
  28  //////////////////////////////////////////////////////////////////////////
  29  // Methods
  30  //////////////////////////////////////////////////////////////////////////
  31  
  32    **
  33    ** Write and then return this.
  34    **
  35    AstWriter w(Obj o)
  36    {
  37      if (needIndent)
  38      {
  39        out.writeChars(Str.spaces(indentation*2))
  40        needIndent = false
  41      }
  42      out.writeChars(o.toStr)
  43      return this
  44    }
  45  
  46    **
  47    ** Write newline and then return this.
  48    **
  49    public AstWriter nl()
  50    {
  51      w("\n")
  52      needIndent = true
  53      out.flush
  54      return this
  55    }
  56  
  57    **
  58    ** Increment the indentation
  59    **
  60    AstWriter indent()
  61    {
  62      indentation++
  63      return this
  64    }
  65  
  66    **
  67    ** Decrement the indentation
  68    **
  69    AstWriter unindent()
  70    {
  71      indentation--
  72      if (indentation < 0) indentation = 0
  73      return this
  74    }
  75  
  76    **
  77    ** Write the source code for the mask of flags with a trailing space.
  78    **
  79    AstWriter flags(Int flags)
  80    {
  81      if (flags & FConst.Public    != 0) w("public ")
  82      if (flags & FConst.Protected != 0) w("protected ")
  83      if (flags & FConst.Private   != 0) w("private ")
  84      if (flags & FConst.Internal  != 0) w("internal ")
  85      if (flags & FConst.Native    != 0) w("native ")
  86      if (flags & FConst.Enum      != 0) w("enum ")
  87      if (flags & FConst.Mixin     != 0) w("mixin ")
  88      if (flags & FConst.Final     != 0) w("final ")
  89      if (flags & FConst.Ctor      != 0) w("new ")
  90      if (flags & FConst.Override  != 0) w("override ")
  91      if (flags & FConst.Abstract  != 0) w("abstract ")
  92      if (flags & FConst.Static    != 0) w("static ")
  93      if (flags & FConst.Storage   != 0) w("storage ")
  94      if (flags & FConst.Virtual   != 0) w("virtual ")
  95  
  96      if (flags & FConst.Synthetic != 0) w("synthetic ")
  97      if (flags & FConst.Getter    != 0) w("getter ")
  98      if (flags & FConst.Setter    != 0) w("setter ")
  99  
 100      return this
 101    }
 102  
 103  //////////////////////////////////////////////////////////////////////////
 104  // Fields
 105  //////////////////////////////////////////////////////////////////////////
 106  
 107    OutStream out
 108    Int indentation := 0
 109    Bool needIndent := false
 110  
 111  }