definition

module org/sugarj/languages/SugarJ
imports org/sugarj/languages/Java 
        org/sugarj/languages/Sdf2
        org/sugarj/languages/Stratego
        org/sugarj/languages/EditorServices
        org/sugarj/languages/Plain

exports
  sorts WATER WATERTOKEN WATERTOKENSTART WATERTOKENSEPARATOR WATERTOKENSTAR

  lexical syntax
    ~[A-Za-z0-9\_]                 -> WATERTOKENSTART     {recover, avoid}
    WATERTOKENSTART [A-Za-z0-9\_]* -> WATERTOKEN          
    ~[A-Za-z0-9\_\ \t\12\r\n\*]    -> WATERTOKENSEPARATOR {recover, avoid}
    "*"                            -> WATERTOKENSTAR      {recover, avoid}
    WATERTOKEN                     -> WATER               
    WATERTOKENSEPARATOR            -> WATER               
    WATERTOKENSTAR                 -> WATER               
                                   -> WATEREOF            

  lexical restrictions
    WATERTOKEN -/- [A-Za-z0-9\_]
    WATERTOKENSTAR -/- [\/]

  context-free syntax
    WATER -> LAYOUT {cons("WATER")}

  (

  sorts WATEREOF

  lexical restrictions
    WATEREOF -/- ~[]

  lexical syntax
    %% Lexical insertion recovery rules                   
                                        -> "="            {recover, cons("INSERTION")}
                                        -> "sorts"        {recover, cons("INSERTION")}
                                        -> "syntax"       {recover, cons("INSERTION")}
                                        -> "variables"    {recover, cons("INSERTION")}
                                        -> "priorities"   {recover, cons("INSERTION")}
                                        -> "restrictions" {recover, cons("INSERTION")}
                                        -> "lexical"      {recover, cons("INSERTION")}
                                        -> "context-free" {recover, cons("INSERTION")}
                                        -> "aliases"      {recover, cons("INSERTION")}
                                        -> "strategies"   {recover, cons("INSERTION")}
                                        -> "signature"    {recover, cons("INSERTION")}
                                        -> "rules"        {recover, cons("INSERTION")}
                                        -> "overlays"     {recover, cons("INSERTION")}
                                        -> "desugarings"  {recover, cons("INSERTION")}
                                        -> "}"            {recover, cons("INSERTION")}
                                        -> "{"            {recover, cons("INSERTION")}

  syntax
    %% Kernel syntax insertion recovery rules   

  )

  (

  sorts SugarDec TypeOrSugarDec SugarMod AnnoOrSugarMod SugarBody SugarDecHead
        SugarCompilationUnit
        ToplevelDeclaration SugarJLayout

  lexical syntax
    [\ \t\12\r\n] -> LAYOUT
    [\ \t\12\r\n]         -> SugarJLayout 
    JavaComment           -> SugarJLayout 
    "%%" line:~[\n]* "\n" -> SugarJLayout {cons("line"), category("Comment")}

  context-free restrictions
    SugarJLayout? -/- [\ \t\12\n\r\%]
    SugarJLayout? -/- [\/] . [\*]
    SugarJLayout? -/- [\/] . [\/]

  context-free start-symbols
  	SugarCompilationUnit

  context-free syntax
    JavaPackageDec? JavaImportDec* TypeOrSugarDec* -> SugarCompilationUnit {prefer, cons("CompilationUnit")}
    JavaTypeDec                                    -> TypeOrSugarDec       {cons("JavaTypeDec")}
    SugarDec                                       -> TypeOrSugarDec       

  context-free syntax
    SugarDecHead SugarBody                 -> SugarDec {cons("SugarDec")}

  context-free syntax
    AnnoOrSugarMod* "sugar" JavaId            -> SugarDecHead       {cons("SugarDecHead")}
    JavaAnno                                  -> AnnoOrSugarMod     
    SugarMod                                  -> AnnoOrSugarMod     
    JavaPublic                                -> SugarMod           

  lexical restrictions
    "sugar" -/- [a-zA-Z0-9\'\-\_]

  context-free syntax
    "{" SugarDef "}"    -> SugarBody       {cons("SugarBody")}

  sorts Grammar Transformation Desugaring SugarDef SugarDefElem

  context-free start-symbols
    SugarDef

  context-free syntax
    Grammar        -> SugarDefElem {cons("grammar-elem")}
    Transformation -> SugarDefElem {cons("transformation-elem")}
    SugarDefElem*  -> SugarDef     

  context-free syntax
    "sorts" Sdf2Symbols                            -> Grammar {cons("sorts")}
    "syntax" Sdf2Productions                       -> Grammar {cons("syntax")}
    "lexical" "syntax" Sdf2Productions             -> Grammar {cons("lexical-syntax")}
    "context-free" "syntax" Sdf2Productions        -> Grammar {cons("context-free-syntax")}
    "variables" Sdf2Productions                    -> Grammar {cons("variables")}
    "lexical" "variables" Sdf2Productions          -> Grammar {cons("lexical-variables")}
    "priorities" Sdf2Priorities                    -> Grammar {cons("priorities")}
    "lexical" "priorities" Sdf2Priorities          -> Grammar {cons("lexical-priorities")}
    "context-free" "priorities" Sdf2Priorities     -> Grammar {cons("context-free-priorities")}
    "restrictions" Sdf2Restrictions                -> Grammar {cons("restrictions")}
    "lexical" "restrictions" Sdf2Restrictions      -> Grammar {cons("lexical-restrictions")}
    "context-free" "restrictions" Sdf2Restrictions -> Grammar {cons("context-free-restrictions")}
    "aliases" Sdf2Aliases                          -> Grammar {cons("aliases")}

  lexical syntax
    "sorts"        -> StrategoId {reject}
    "lexical"      -> StrategoId {reject}
    "context-free" -> StrategoId {reject}
    "variables"    -> StrategoId {reject}
    "priorities"   -> StrategoId {reject}
    "restrictions" -> StrategoId {reject}
    "aliases"      -> StrategoId {reject}

  context-free syntax
    "strategies" StrategoDef*   -> Transformation {cons("Strategies")}
    "signature" StrategoSdecl*  -> Transformation {cons("Signature")}
    "rules" StrategoDef*        -> Transformation {cons("Rules")}
    "overlays" StrategoOverlay* -> Transformation {cons("Overlays")}

  lexical syntax
    "desugarings" -> StrategoKeyword 

  context-free syntax
    "desugarings" Desugaring* -> Transformation {cons("Desugarings")}

  context-free syntax
    StrategoStrategy                                               -> Desugaring {cons("PureDesugaring")}
    StrategoStrategy ":" Sdf2Symbol "->" Sdf2Symbol Sdf2Attributes -> Desugaring {cons("InjectDesugaring")}

  lexical restrictions
    "desugarings" -/- [a-zA-Z0-9\'\-\_]

  sorts EditorServicesDecHead EditorServicesBody

  context-free syntax
    EditorServicesDecHead EditorServicesBody   -> SugarDec              {cons("EditorServicesDec")}
    AnnoOrSugarMod* "editor" "services" JavaId -> EditorServicesDecHead {cons("EditorServicesDecHead")}
    "{" EditorServiceSection* "}"              -> EditorServicesBody    {cons("EditorServicesBody")}

  context-free syntax
    ChecksDecHead ChecksBody   -> ToplevelDeclaration              {cons("ChecksDec")}
    AnnoOrSugarMod* "analysis" JavaId -> ChecksDecHead {cons("ChecksDecHead")}
    AnnoOrSugarMod* "checks" JavaId -> ChecksDecHead {cons("ChecksDecHead")}
    "{" CheckingRule* "}"              -> ChecksBody    {cons("ChecksBody")}

  context-free syntax
    "errors" CheckingRuleBody*                          -> CheckingRule     {cons("CheckingRuleErrors")}
    "warnings" CheckingRuleBody*                        -> CheckingRule     {cons("CheckingRuleWarnings")}
    "notes" CheckingRuleBody*                           -> CheckingRule     {cons("CheckingRuleNotes")}
    StrategoTerm "=" StrategoTerm                          -> CheckingRuleBody {cons("RuleNoCond")}
    StrategoTerm "=" StrategoTerm "where" StrategoStrategy -> CheckingRuleBody {cons("Rule")}
    StrategoTerm "=" StrategoTerm StrategoRuleCond+        -> CheckingRuleBody {cons("Rule"), avoid}

    "rules" StrategoDef*        -> CheckingRule {cons("Rules")}

  lexical syntax
         -> "}" {recover, reject}

  )

  %% moved from initgrammar  
  context-free syntax %% ToplevelDeclaration
    JavaPackageDec -> ToplevelDeclaration
    JavaImportDec -> ToplevelDeclaration
    JavaTypeDec -> ToplevelDeclaration
    SugarDec -> ToplevelDeclaration

