{- |
Description: Example implementation of a simplified version of the Java grammar.
License: GNU LGPLv3
Maintainer: paul.bittner@uni-ulm.de
Example implementation of a context free grammar.
This example represents a simplified subset of the Java programming language.
It contains the 'Grammar' implementation as well as functions to construct an 'AST' in this sub-language.
-}
module SimpleJava where
import Tree
import Control.Monad.State ( State )
import UUID ( UUID )
import AST
import Grammar
import ASTPrettyPrinter
import Data.List ( intersperse )
data SimpleJavaGrammar =
SJava_MethodDef
| SJava_ParametersDef
| SJava_Args
| SJava_Statements
| SJava_ExprStatement
| SJava_Assignment
| SJava_Return
| SJava_Condition
| SJava_FuncCall
| SJava_Expression
| SJava_UnaryOp
| SJava_BinaryOp
| SJava_VarDecl
| SJava_VarRef
| SJava_Literal
| SJava_Type
| SJava_File
deriving (SimpleJavaGrammar -> SimpleJavaGrammar -> Bool
(SimpleJavaGrammar -> SimpleJavaGrammar -> Bool)
-> (SimpleJavaGrammar -> SimpleJavaGrammar -> Bool)
-> Eq SimpleJavaGrammar
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SimpleJavaGrammar -> SimpleJavaGrammar -> Bool
$c/= :: SimpleJavaGrammar -> SimpleJavaGrammar -> Bool
== :: SimpleJavaGrammar -> SimpleJavaGrammar -> Bool
$c== :: SimpleJavaGrammar -> SimpleJavaGrammar -> Bool
Eq, Int -> SimpleJavaGrammar -> ShowS
[SimpleJavaGrammar] -> ShowS
SimpleJavaGrammar -> String
(Int -> SimpleJavaGrammar -> ShowS)
-> (SimpleJavaGrammar -> String)
-> ([SimpleJavaGrammar] -> ShowS)
-> Show SimpleJavaGrammar
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SimpleJavaGrammar] -> ShowS
$cshowList :: [SimpleJavaGrammar] -> ShowS
show :: SimpleJavaGrammar -> String
$cshow :: SimpleJavaGrammar -> String
showsPrec :: Int -> SimpleJavaGrammar -> ShowS
$cshowsPrec :: Int -> SimpleJavaGrammar -> ShowS
Show)
type SJavaAST a = AST SimpleJavaGrammar a
type SSJavaAST = SJavaAST String
type SJavaState = Tree (State UUID (Node SimpleJavaGrammar String))
sjava_methoddef :: String -> String -> [(String, String)] -> [SJavaState] -> SJavaState
sjava_methoddef :: String
-> String -> [(String, String)] -> [SJavaState] -> SJavaState
sjava_methoddef rettype :: String
rettype name :: String
name params :: [(String, String)]
params content :: [SJavaState]
content =
(State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
name SimpleJavaGrammar
SJava_MethodDef) [
String -> SJavaState
sjava_type String
rettype,
[(String, String)] -> SJavaState
sjava_parametersdef [(String, String)]
params,
[SJavaState] -> SJavaState
sjava_statements [SJavaState]
content
])
sjava_parametersdef :: [(String, String)] -> SJavaState
sjava_parametersdef :: [(String, String)] -> SJavaState
sjava_parametersdef params :: [(String, String)]
params = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_ParametersDef) ([SJavaState] -> SJavaState) -> [SJavaState] -> SJavaState
forall a b. (a -> b) -> a -> b
$ SJavaState -> SJavaState
sjava_expr(SJavaState -> SJavaState)
-> ((String, String) -> SJavaState)
-> (String, String)
-> SJavaState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((String -> String -> SJavaState) -> (String, String) -> SJavaState
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry String -> String -> SJavaState
sjava_vardecl) ((String, String) -> SJavaState)
-> [(String, String)] -> [SJavaState]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(String, String)]
params
sjava_args :: [SJavaState] -> SJavaState
sjava_args :: [SJavaState] -> SJavaState
sjava_args params :: [SJavaState]
params = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_Args) ([SJavaState] -> SJavaState) -> [SJavaState] -> SJavaState
forall a b. (a -> b) -> a -> b
$ SJavaState -> SJavaState
sjava_expr(SJavaState -> SJavaState)
-> (SJavaState -> SJavaState) -> SJavaState -> SJavaState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.SJavaState -> SJavaState
sjava_expr (SJavaState -> SJavaState) -> [SJavaState] -> [SJavaState]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SJavaState]
params
sjava_statements :: [SJavaState] -> SJavaState
sjava_statements :: [SJavaState] -> SJavaState
sjava_statements statements :: [SJavaState]
statements = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_Statements) [SJavaState]
statements
sjava_exprstatement :: SJavaState -> SJavaState
sjava_exprstatement :: SJavaState -> SJavaState
sjava_exprstatement expression :: SJavaState
expression = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_ExprStatement) [SJavaState -> SJavaState
sjava_expr SJavaState
expression]
sjava_assignment :: SJavaState -> String -> SJavaState -> SJavaState
sjava_assignment :: SJavaState -> String -> SJavaState -> SJavaState
sjava_assignment lhs :: SJavaState
lhs op :: String
op expr :: SJavaState
expr = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
op SimpleJavaGrammar
SJava_Assignment) [SJavaState
lhs, SJavaState -> SJavaState
sjava_expr SJavaState
expr]
sjava_return :: SJavaState -> SJavaState
sjava_return :: SJavaState -> SJavaState
sjava_return expr :: SJavaState
expr = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_Return) [SJavaState -> SJavaState
sjava_expr SJavaState
expr]
sjava_condition :: SJavaState -> [SJavaState] -> SJavaState
sjava_condition :: SJavaState -> [SJavaState] -> SJavaState
sjava_condition cond :: SJavaState
cond block :: [SJavaState]
block =
State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_Condition) [
SJavaState -> SJavaState
sjava_expr SJavaState
cond,
[SJavaState] -> SJavaState
sjava_statements [SJavaState]
block
]
sjava_funccall :: String -> [SJavaState] -> SJavaState
sjava_funccall :: String -> [SJavaState] -> SJavaState
sjava_funccall name :: String
name params :: [SJavaState]
params =
State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
name SimpleJavaGrammar
SJava_FuncCall) [
[SJavaState] -> SJavaState
sjava_args [SJavaState]
params
]
sjava_expr :: SJavaState -> SJavaState
sjava_expr :: SJavaState -> SJavaState
sjava_expr inner :: SJavaState
inner = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node "Expression" SimpleJavaGrammar
SJava_Expression) [SJavaState
inner]
sjava_unaryop :: String -> SJavaState -> SJavaState
sjava_unaryop :: String -> SJavaState -> SJavaState
sjava_unaryop op :: String
op expr :: SJavaState
expr = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
op SimpleJavaGrammar
SJava_UnaryOp) [SJavaState -> SJavaState
sjava_expr SJavaState
expr]
sjava_binaryop :: SJavaState -> String -> SJavaState -> SJavaState
sjava_binaryop :: SJavaState -> String -> SJavaState -> SJavaState
sjava_binaryop lhs :: SJavaState
lhs op :: String
op rhs :: SJavaState
rhs =
State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
op SimpleJavaGrammar
SJava_BinaryOp) [
SJavaState -> SJavaState
sjava_expr SJavaState
lhs,
SJavaState -> SJavaState
sjava_expr SJavaState
rhs
]
sjava_vardecl :: String -> String -> SJavaState
sjava_vardecl :: String -> String -> SJavaState
sjava_vardecl vartype :: String
vartype varname :: String
varname =
(State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
forall a. Monoid a => a
mempty SimpleJavaGrammar
SJava_VarDecl)) [
String -> SJavaState
sjava_type String
vartype,
String -> SJavaState
sjava_literal String
varname
]
sjava_varref :: String -> SJavaState
sjava_varref :: String -> SJavaState
sjava_varref name :: String
name = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
name SimpleJavaGrammar
SJava_VarRef) []
sjava_literal :: String -> SJavaState
sjava_literal :: String -> SJavaState
sjava_literal val :: String
val = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
val SimpleJavaGrammar
SJava_Literal) []
sjava_type :: String -> SJavaState
sjava_type :: String -> SJavaState
sjava_type val :: String
val = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
val SimpleJavaGrammar
SJava_Type) []
sjava_file :: String -> [SJavaState] -> SJavaState
sjava_file :: String -> [SJavaState] -> SJavaState
sjava_file name :: String
name content :: [SJavaState]
content = State Int (Node SimpleJavaGrammar String)
-> [SJavaState] -> SJavaState
forall a. a -> [Tree a] -> Tree a
Tree (String
-> SimpleJavaGrammar -> State Int (Node SimpleJavaGrammar String)
forall g a. Grammar g => a -> g -> State Int (Node g a)
node String
name SimpleJavaGrammar
SJava_File) [SJavaState]
content
instance Grammar SimpleJavaGrammar where
nodetypeof :: SimpleJavaGrammar -> NodeType
nodetypeof SJava_MethodDef = NodeType
Optional
nodetypeof SJava_Return = NodeType
Optional
nodetypeof SJava_File = NodeType
Optional
nodetypeof SJava_ExprStatement = NodeType
Optional
nodetypeof SJava_VarDecl = NodeType
Mandatory
nodetypeof SJava_Condition = NodeType
Wrapper
nodetypeof SJava_UnaryOp = NodeType
Wrapper
nodetypeof SJava_Type = NodeType
Optional
nodetypeof SJava_Expression = NodeType
Mandatory
nodetypeof SJava_ParametersDef = NodeType
Mandatory
nodetypeof SJava_Args = NodeType
Mandatory
nodetypeof SJava_Statements = NodeType
Mandatory
nodetypeof SJava_BinaryOp = NodeType
Optional
nodetypeof SJava_FuncCall = NodeType
Optional
nodetypeof SJava_VarRef = NodeType
Optional
nodetypeof SJava_Literal = NodeType
Optional
nodetypeof SJava_Assignment = NodeType
Mandatory
instance ASTPrettyPrinter SimpleJavaGrammar where
prettyPrint :: b
-> (Node SimpleJavaGrammar a -> Int -> b)
-> (Node SimpleJavaGrammar a -> String -> b)
-> (Node SimpleJavaGrammar a -> b)
-> AST SimpleJavaGrammar a
-> b
prettyPrint i :: b
i indentGenerator :: Node SimpleJavaGrammar a -> Int -> b
indentGenerator prtStrWithContext :: Node SimpleJavaGrammar a -> String -> b
prtStrWithContext prtNode :: Node SimpleJavaGrammar a -> b
prtNode (Tree n :: Node SimpleJavaGrammar a
n children :: [AST SimpleJavaGrammar a]
children) =
[b] -> b
forall a. Monoid a => [a] -> a
mconcat ([b] -> b) -> [b] -> b
forall a b. (a -> b) -> a -> b
$
let indentLen :: Int
indentLen = 2
indent :: b
indent = b -> b -> b
forall a. Monoid a => a -> a -> a
mappend b
i (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ Node SimpleJavaGrammar a -> Int -> b
indentGenerator Node SimpleJavaGrammar a
n Int
indentLen
prtStr :: String -> b
prtStr = Node SimpleJavaGrammar a -> String -> b
prtStrWithContext Node SimpleJavaGrammar a
n
me :: b
me = Node SimpleJavaGrammar a -> b
prtNode Node SimpleJavaGrammar a
n
showList :: String -> [AST SimpleJavaGrammar a] -> b
showList sep :: String
sep l :: [AST SimpleJavaGrammar a]
l = b -> [AST SimpleJavaGrammar a] -> b
showList' (String -> b
prtStr String
sep) [AST SimpleJavaGrammar a]
l
showList' :: b -> [AST SimpleJavaGrammar a] -> b
showList' sep :: b
sep l :: [AST SimpleJavaGrammar a]
l = [b] -> b
forall a. Monoid a => [a] -> a
mconcat ([b] -> b) -> [b] -> b
forall a b. (a -> b) -> a -> b
$ b -> [b] -> [b]
forall a. a -> [a] -> [a]
intersperse b
sep ([b] -> [b]) -> [b] -> [b]
forall a b. (a -> b) -> a -> b
$ (b
-> (Node SimpleJavaGrammar a -> Int -> b)
-> (Node SimpleJavaGrammar a -> String -> b)
-> (Node SimpleJavaGrammar a -> b)
-> AST SimpleJavaGrammar a
-> b
forall b g a.
(Monoid b, ASTPrettyPrinter g) =>
b
-> (Node g a -> Int -> b)
-> (Node g a -> String -> b)
-> (Node g a -> b)
-> AST g a
-> b
showCodeAs b
indent Node SimpleJavaGrammar a -> Int -> b
indentGenerator Node SimpleJavaGrammar a -> String -> b
prtStrWithContext Node SimpleJavaGrammar a -> b
prtNode) (AST SimpleJavaGrammar a -> b) -> [AST SimpleJavaGrammar a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [AST SimpleJavaGrammar a]
l
showListNoIndentIncrease :: String -> [AST SimpleJavaGrammar a] -> b
showListNoIndentIncrease sep :: String
sep l :: [AST SimpleJavaGrammar a]
l = [b] -> b
forall a. Monoid a => [a] -> a
mconcat ([b] -> b) -> [b] -> b
forall a b. (a -> b) -> a -> b
$ b -> [b] -> [b]
forall a. a -> [a] -> [a]
intersperse (String -> b
prtStr String
sep) ([b] -> [b]) -> [b] -> [b]
forall a b. (a -> b) -> a -> b
$ b
-> (Node SimpleJavaGrammar a -> Int -> b)
-> (Node SimpleJavaGrammar a -> String -> b)
-> (Node SimpleJavaGrammar a -> b)
-> AST SimpleJavaGrammar a
-> b
forall b g a.
(Monoid b, ASTPrettyPrinter g) =>
b
-> (Node g a -> Int -> b)
-> (Node g a -> String -> b)
-> (Node g a -> b)
-> AST g a
-> b
showCodeAs b
i Node SimpleJavaGrammar a -> Int -> b
indentGenerator Node SimpleJavaGrammar a -> String -> b
prtStrWithContext Node SimpleJavaGrammar a -> b
prtNode (AST SimpleJavaGrammar a -> b) -> [AST SimpleJavaGrammar a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [AST SimpleJavaGrammar a]
l
showHead :: b
showHead = b
-> (Node SimpleJavaGrammar a -> Int -> b)
-> (Node SimpleJavaGrammar a -> String -> b)
-> (Node SimpleJavaGrammar a -> b)
-> AST SimpleJavaGrammar a
-> b
forall b g a.
(Monoid b, ASTPrettyPrinter g) =>
b
-> (Node g a -> Int -> b)
-> (Node g a -> String -> b)
-> (Node g a -> b)
-> AST g a
-> b
showCodeAs b
indent Node SimpleJavaGrammar a -> Int -> b
indentGenerator Node SimpleJavaGrammar a -> String -> b
prtStrWithContext Node SimpleJavaGrammar a -> b
prtNode (AST SimpleJavaGrammar a -> b) -> AST SimpleJavaGrammar a -> b
forall a b. (a -> b) -> a -> b
$ [AST SimpleJavaGrammar a] -> AST SimpleJavaGrammar a
forall a. [a] -> a
head [AST SimpleJavaGrammar a]
children
in case Node SimpleJavaGrammar a -> SimpleJavaGrammar
forall g a. Node g a -> g
grammartype Node SimpleJavaGrammar a
n of
SJava_MethodDef -> [b
indent, b
showHead, String -> b
prtStr " ", b
me, String -> [AST SimpleJavaGrammar a] -> b
showList " " ([AST SimpleJavaGrammar a] -> b) -> [AST SimpleJavaGrammar a] -> b
forall a b. (a -> b) -> a -> b
$ [AST SimpleJavaGrammar a] -> [AST SimpleJavaGrammar a]
forall a. [a] -> [a]
tail [AST SimpleJavaGrammar a]
children]
SJava_ParametersDef -> [String -> b
prtStr "(", String -> [AST SimpleJavaGrammar a] -> b
showList ", " [AST SimpleJavaGrammar a]
children, String -> b
prtStr ")"]
SJava_Args -> [String -> b
prtStr "(", String -> [AST SimpleJavaGrammar a] -> b
showList ", " [AST SimpleJavaGrammar a]
children, String -> b
prtStr ")"]
SJava_Statements -> [
String -> b
prtStr "{\n",
if [AST SimpleJavaGrammar a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [AST SimpleJavaGrammar a]
children
then b
forall a. Monoid a => a
mempty
else (b -> b -> b
forall a. Monoid a => a -> a -> a
mappend (String -> [AST SimpleJavaGrammar a] -> b
showListNoIndentIncrease "\n" [AST SimpleJavaGrammar a]
children) (String -> b
prtStr "\n")),
b
i, String -> b
prtStr "}"]
SJava_ExprStatement -> [b
indent, String -> [AST SimpleJavaGrammar a] -> b
showList " " [AST SimpleJavaGrammar a]
children, String -> b
prtStr ";"]
SJava_Assignment -> [b -> [AST SimpleJavaGrammar a] -> b
showList' ([b] -> b
forall a. Monoid a => [a] -> a
mconcat [String -> b
prtStr" ",b
me,String -> b
prtStr" "]) [AST SimpleJavaGrammar a]
children]
SJava_Return -> [b
indent, String -> b
prtStr "return ", String -> [AST SimpleJavaGrammar a] -> b
showList " " [AST SimpleJavaGrammar a]
children, String -> b
prtStr ";"]
SJava_Condition -> [b
indent, String -> b
prtStr "if (", b
showHead, String -> b
prtStr ") ", String -> [AST SimpleJavaGrammar a] -> b
showList " " ([AST SimpleJavaGrammar a] -> b) -> [AST SimpleJavaGrammar a] -> b
forall a b. (a -> b) -> a -> b
$ [AST SimpleJavaGrammar a] -> [AST SimpleJavaGrammar a]
forall a. [a] -> [a]
tail [AST SimpleJavaGrammar a]
children]
SJava_FuncCall -> [b
me, String -> [AST SimpleJavaGrammar a] -> b
showList ", " [AST SimpleJavaGrammar a]
children]
SJava_Expression -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> [b]) -> b -> [b]
forall a b. (a -> b) -> a -> b
$ if [AST SimpleJavaGrammar a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AST SimpleJavaGrammar a]
children Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 then b
showHead else String -> b
forall a. HasCallStack => String -> a
error "Expressios can only have one child"
SJava_UnaryOp -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> [b]) -> b -> [b]
forall a b. (a -> b) -> a -> b
$ if [AST SimpleJavaGrammar a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AST SimpleJavaGrammar a]
children Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 then b -> b -> b
forall a. Monoid a => a -> a -> a
mappend b
me b
showHead else String -> b
forall a. HasCallStack => String -> a
error "Unary operations can only have one child"
SJava_BinaryOp -> if [AST SimpleJavaGrammar a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [AST SimpleJavaGrammar a]
children Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 2 then [b
showHead, String -> b
prtStr " ", b
me, String -> b
prtStr " ", b
-> (Node SimpleJavaGrammar a -> Int -> b)
-> (Node SimpleJavaGrammar a -> String -> b)
-> (Node SimpleJavaGrammar a -> b)
-> AST SimpleJavaGrammar a
-> b
forall b g a.
(Monoid b, ASTPrettyPrinter g) =>
b
-> (Node g a -> Int -> b)
-> (Node g a -> String -> b)
-> (Node g a -> b)
-> AST g a
-> b
showCodeAs b
indent Node SimpleJavaGrammar a -> Int -> b
indentGenerator Node SimpleJavaGrammar a -> String -> b
prtStrWithContext Node SimpleJavaGrammar a -> b
prtNode (AST SimpleJavaGrammar a -> b) -> AST SimpleJavaGrammar a -> b
forall a b. (a -> b) -> a -> b
$ [AST SimpleJavaGrammar a] -> AST SimpleJavaGrammar a
forall a. [a] -> a
head ([AST SimpleJavaGrammar a] -> AST SimpleJavaGrammar a)
-> [AST SimpleJavaGrammar a] -> AST SimpleJavaGrammar a
forall a b. (a -> b) -> a -> b
$ [AST SimpleJavaGrammar a] -> [AST SimpleJavaGrammar a]
forall a. [a] -> [a]
tail [AST SimpleJavaGrammar a]
children] else String -> [b]
forall a. HasCallStack => String -> a
error "Binary operations must have exactly two children"
SJava_VarDecl -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> [b]) -> b -> [b]
forall a b. (a -> b) -> a -> b
$ String -> [AST SimpleJavaGrammar a] -> b
showList " " [AST SimpleJavaGrammar a]
children
SJava_VarRef -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return b
me
SJava_Literal -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return b
me
SJava_Type -> b -> [b]
forall (m :: * -> *) a. Monad m => a -> m a
return b
me
SJava_File -> [b
indent, String -> b
prtStr "FILE [", b
me, String -> b
prtStr (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$ "] {\n", String -> [AST SimpleJavaGrammar a] -> b
showList "\n" [AST SimpleJavaGrammar a]
children, String -> b
prtStr "\n", b
indent, String -> b
prtStr "}"]