LicenseGNU LGPLv3
Maintainerpaul.bittner@uni-ulm.de
Safe HaskellSafe

SimpleJava

Description

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.

Synopsis

Documentation

data SimpleJavaGrammar Source #

Simplified subset of the Java grammar.

Constructors

SJava_MethodDef

Rule for method definitions.

SJava_ParametersDef

Rule for a parameters list (e.g., in a method declaration).

SJava_Args

Rule for arguments passed to a method call (e.g., (1 + 2, "arg2", null)).

SJava_Statements

Rule for a sequential list of statements (e.g., inside a method).

SJava_ExprStatement

Rule for a statement that is a single expression (e.g., x += 3 that also returns the new value of x is an expression statement).

SJava_Assignment

Rule for assignments of variables (e.g., x = 3).

SJava_Return

Rule for return statements.

SJava_Condition

Rule for conditions (if).

SJava_FuncCall

Rule for function calls.

SJava_Expression

Rule for expressions (e.g., 1 + 1).

SJava_UnaryOp

Rule for unary operations (e.g., ! or -).

SJava_BinaryOp

Rule for binary operations (e.g., + or *).

SJava_VarDecl

Rule for variable declarations (e.g., int x;).

SJava_VarRef

Rule for variable references.

SJava_Literal

Rule for literals.

SJava_Type

Rule for types (e.g., int in int x;).

SJava_File

Rule that represents an entire file. Usually the root of the AST.

Instances

Instances details
Eq SimpleJavaGrammar Source # 
Instance details

Defined in SimpleJava

Show SimpleJavaGrammar Source # 
Instance details

Defined in SimpleJava

Methods

showsPrec :: Int -> SimpleJavaGrammar -> ShowS

show :: SimpleJavaGrammar -> String

showList :: [SimpleJavaGrammar] -> ShowS

Grammar SimpleJavaGrammar Source #

Define optionality for all node types in our Java grammar

Instance details

Defined in SimpleJava

ASTPrettyPrinter SimpleJavaGrammar Source # 
Instance details

Defined in SimpleJava

Methods

prettyPrint :: Monoid b => b -> (Node SimpleJavaGrammar a -> Int -> b) -> (Node SimpleJavaGrammar a -> String -> b) -> (Node SimpleJavaGrammar a -> b) -> AST SimpleJavaGrammar a -> b Source #

type SJavaAST a = AST SimpleJavaGrammar a Source #

An AST build from the SimpleJavaGrammar.

type SSJavaAST = SJavaAST String Source #

An AST build from the SimpleJavaGrammar containing Strings as values.

type SJavaState = Tree (State UUID (Node SimpleJavaGrammar String)) Source #

A SSJavaAST whose nodes are not yet assigned their UUIDs (they are still states waiting for evaluation).

sjava_methoddef :: String -> String -> [(String, String)] -> [SJavaState] -> SJavaState Source #

Construct a method definition with given (1) return type, (2) name, (3) parameters (pairs of type and name), and (4) the given content (list of statements).

sjava_parametersdef :: [(String, String)] -> SJavaState Source #

Construct a definition of parameters subtree from the given pairs of type and name.

sjava_args :: [SJavaState] -> SJavaState Source #

Construct an arguments subtree from the given list of subtrees (e.g., (1 + 2, "arg2", null)).

sjava_statements :: [SJavaState] -> SJavaState Source #

Construct a statements block from a list of statements.

sjava_exprstatement :: SJavaState -> SJavaState Source #

Wrap the given subtree in an expression statement. The given tree should be an expression. Pseudocode example: sjava_exprstatement 'x = 3 == 'x + 3;'.

sjava_assignment :: SJavaState -> String -> SJavaState -> SJavaState Source #

Constructs an assignment where parameter

  1. represents the left side of the assignment (e.g., a variable that should be assigned a value),
  2. is the operator's name to use (e.g., "="), and
  3. is the expression to assign (e.g. "1 + 2").

sjava_return :: SJavaState -> SJavaState Source #

Constructs a return statement that returns the given expression.

sjava_condition :: SJavaState -> [SJavaState] -> SJavaState Source #

Constructs an if-block (without else case) with the given condition (first argument) and sequence of statements to run when the condition is met.

sjava_funccall :: String -> [SJavaState] -> SJavaState Source #

Constructs a call to a function the given name and a list of arguments to pass.

sjava_expr :: SJavaState -> SJavaState Source #

Wraps an tree in an expression. For example, a literal is an expression. Thus a literal can be used anywhere an expression required but should be wrapped into an expression first.

sjava_unaryop :: String -> SJavaState -> SJavaState Source #

Constructs a unary operation (e.g. -3) from a given name (-) that is applied to the given expression (3).

sjava_binaryop :: SJavaState -> String -> SJavaState -> SJavaState Source #

Constructs a binary operator from a given name and two expressions.

sjava_vardecl :: String -> String -> SJavaState Source #

Constructs a variable declaration (e.g., int x;) from a type (e.g., int) and a name (e.g., x).

sjava_varref :: String -> SJavaState Source #

Constructs a reference to a variable with the given name. The returned tree is a leaf.

sjava_literal :: String -> SJavaState Source #

Constructs a literal with the given name. The returned tree is a leaf.

sjava_type :: String -> SJavaState Source #

Constructs a type with the given name. The returned tree is a leaf.

sjava_file :: String -> [SJavaState] -> SJavaState Source #

Constructs a file with the given name and content.