2015-11-18 00:29:55 +03:00
module Syntax where
2016-05-26 19:58:04 +03:00
import Prologue
2016-01-06 19:56:58 +03:00
import Data.OrderedMap
2015-12-15 21:29:58 +03:00
import qualified Data.Text as T
2015-11-18 00:29:55 +03:00
2015-11-27 17:41:43 +03:00
-- | A node in an abstract syntax tree.
data Syntax
a -- ^ The type of leaves in the syntax tree, typically String, but possibly some datatype representing different leaves more precisely.
f -- ^ The type representing another level of the tree, e.g. the children of branches. Often Cofree or Fix or similar.
=
-- | A terminal syntax node, e.g. an identifier, or atomic literal.
2015-11-18 03:17:42 +03:00
Leaf a
2015-11-27 17:41:43 +03:00
-- | An ordered branch of child nodes, expected to be variadic in the grammar, e.g. a list of statements or uncurried function parameters.
2015-11-18 03:17:42 +03:00
| Indexed [ f ]
2015-11-27 17:41:43 +03:00
-- | An ordered branch of child nodes, expected to be of fixed length in the grammar, e.g. a binary operator & its operands.
2015-11-18 03:17:42 +03:00
| Fixed [ f ]
2015-11-27 17:41:43 +03:00
-- | A branch of child nodes indexed by some String identity. This is useful for identifying e.g. methods & properties in a class scope by their names. Note that comments can generally occur in these scopes as well; one strategy for dealing with this is to identify comments by their text in the source.
2015-12-15 21:29:58 +03:00
| Keyed ( OrderedMap T . Text f )
2016-06-09 00:18:54 +03:00
-- | A function call has an identifier where f is a (Leaf a) and a list of arguments.
| FunctionCall f [ f ]
2016-06-17 23:32:07 +03:00
-- | A ternary has a condition, a true case and a false case
| Ternary { ternaryCondition :: f , ternaryCases :: [ f ] }
2016-06-10 20:11:32 +03:00
-- | A function has a list of expressions.
2016-06-10 22:10:37 +03:00
| Function { id :: ( Maybe f ) , params :: ( Maybe f ) , expressions :: f }
2016-06-14 01:32:41 +03:00
-- | An assignment has an identifier where f can be a member access, and the value is another syntax element (function call, leaf, etc.)
2016-06-14 00:32:08 +03:00
| Assignment { assignmentId :: f , value :: f }
2016-06-17 22:57:51 +03:00
-- | A math assignment represents expressions whose operator classifies as mathy (e.g. += or *=).
| MathAssignment { mathAssignmentId :: f , value :: f }
2016-06-14 01:32:23 +03:00
-- | A member access contains a syntax, and another syntax that identifies a property or value in the first syntax.
-- | e.g. in Javascript x.y represents a member access syntax.
| MemberAccess { memberId :: f , property :: f }
2016-06-13 00:26:20 +03:00
-- | A method call consisting of its target, the method name, and the parameters passed to the method.
-- | e.g. in Javascript console.log('hello') represents a method call.
| MethodCall { targetId :: f , methodId :: f , methodParams :: f }
2016-06-15 00:37:02 +03:00
-- | The list of arguments to a method call.
-- | TODO: It might be worth removing this and using Fixed instead.
2016-06-14 19:53:35 +03:00
| Args [ f ]
2016-06-16 00:58:48 +03:00
-- | A variable declaration. e.g. var foo;
2016-06-15 03:44:10 +03:00
| VarDecl f
2016-06-16 00:58:48 +03:00
-- | A variable assignment in a variable declaration. var foo = bar;
2016-06-15 03:44:10 +03:00
| VarAssignment { varId :: f , varValue :: f }
2016-06-17 22:39:18 +03:00
-- | A subscript access contains a syntax, and another syntax that indefies a property or value in the first syntax.
-- | e.g. in Javascript x["y"] represents a subscript access syntax.
| SubscriptAccess { subscriptId :: f , subscriptElement :: f }
2016-06-16 01:20:58 +03:00
| Switch { switchExpr :: f , cases :: [ f ] }
| Case { caseExpr :: f , caseStatements :: f }
2015-11-25 18:58:47 +03:00
deriving ( Functor , Show , Eq , Foldable , Traversable )