/*-----------------------------------------------------------------
Config.atg
2005-06 Philipp Grasboeck
-------------------------------------------------------------------*/

using Printy.Library;

COMPILER PrintyConfig

  public Config config;
  public HyperConfig htmlConfig;

/*-------------------------------------------------------------------------*/

CHARACTERS
  digit      = "0123456789".
  tab        = '\t'.
  cr         = '\r'.
  lf         = '\n'.
  eol        = cr + lf.

TOKENS
  number = ['-'] digit { digit }.

COMMENTS FROM "/*" TO "*/" NESTED
COMMENTS FROM "//" TO cr lf

IGNORE eol + tab

/*-------------------------------------------------------------------------*/

PRODUCTIONS

PrintyConfig
=
  { Rule ';' }
.

Rule = ConfigRule | HyperConfigRule.

ConfigRule
=                                       (. if (config == null) errors.Exception("Config not set."); .)
  ( SpaceRule
  | BreakRule
  | OpenRule
  | CloseRule
  | HideRule
  | OverlapRule
  )
.

HyperConfigRule
=                                       (. if (htmlConfig == null) errors.Exception("HyperConfig not set."); .)
  ( StyleRule
  | ClassRule
  )
.

SpaceRule                               (. int symbol, follow; .)
= "space"
  SymbolAllIndent<out symbol>
  SymbolAll<out follow>
  number                                (. config.PutSpace(symbol,follow,int.Parse(t.val)); .)
.

BreakRule                               (. int symbol, follow; .)
= "break"
  SymbolAll<out symbol>
  SymbolAll<out follow>
  ( number                              (. config.PutBreak(symbol,follow,int.Parse(t.val)); .)
  | "default"                           (. config.PutBreak(symbol,follow,Config.BREAK_DEFAULT); .)
  | "always"                            (. config.PutBreak(symbol,follow,Config.BREAK_ALWAYS); .)
  | "never"                             (. config.PutBreak(symbol,follow,Config.BREAK_NEVER); .)
  )
.

OpenRule
= "open"
  ( BitSet<out config.openSet>
  | Bit<ref config.openSet>
  )
.

CloseRule
= "close"
  ( BitSet<out config.closeSet>
  | Bit<ref config.closeSet>
  )
.

HideRule
= "hide"
  ( BitSet<out config.hideSet>
  | Bit<ref config.hideSet>
  )
.

OverlapRule
= "overlap"
  ( BitSet<out config.overlapSet>
  | Bit<ref config.overlapSet>
  )
.

StyleRule
= "style"
  ( BitSet<out htmlConfig.styleSet>
  | Bit<ref htmlConfig.styleSet>
  )
.

ClassRule                               (. string text, className; int symbol = Config.ALL; .)
= "class"
  Text<out className>
  [ SymbolAll<out symbol> ]
  Text<out text>                        (. htmlConfig.PutStyleClass(text,symbol,className); .)
  { Text<out text>                      (. htmlConfig.PutStyleClass(text,symbol,className); .)
  }
.

Bit<ref BitSet vector>                  (. int symbol; .)
= SymbolAll<out symbol>                 (. if (symbol == Config.ALL) vector |= true; else vector += symbol; .)
.

BitSet<out BitSet vector>               (. int symbol; .)
=                                       (. vector = new BitSet(); .)
  '['
  { Symbol<out symbol>                  (. vector += symbol; .)
  }
  ']'
.

SymbolAll<out int type>
=                                       (. type = 0; .)
  ( "ALL"                               (. type = Config.ALL; .)
  | Symbol<out type>
  )
.

SymbolAllIndent<out int type>
=                                       (. type = 0; .)
  ( "INDENT"                            (. type = Config.INDENT; .)
  | SymbolAll<out type>
  )
.

Symbol<out int type>
=                                       (. type = 0; .)
  ( "Production"                        (. type = Config.Production; .)
  | "Symbol"                            (. type = Config.Symbol; .)
  | "Literal"                           (. type = Config.Literal; .)
  | "Open"                              (. type = Config.Open; .)
  | "Close"                             (. type = Config.Close; .)
  | "Assign"                            (. type = Config.Assign; .)
  | "Separator"                         (. type = Config.Separator; .)
  | "Delimiter"                         (. type = Config.Delimiter; .)
  | "InnerComment"                      (. type = Config.InnerComment; .)
  | "OuterComment"                      (. type = Config.OuterComment; .)
  | "Attribute"                         (. type = Config.Attribute; .)
  | "Resolver"                          (. type = Config.Resolver; .)
  | "Prolog"                            (. type = Config.Prolog; .)
  | "Epilog"                            (. type = Config.Epilog; .)
  | "Action"                            (. type = Config.Action; .)
  | "LineNumber"                        (. type = Config.LineNumber; .)
  )
.

Text<out string text>                   (. Token start = la; .)
= '<'
  { ANY }
  '>'                                   (. text = scanner.buffer.GetString(start.pos + 1,t.pos); .)
.

END PrintyConfig.