stan-0.2.0.0: Haskell STatic ANalyser

Copyright | (c) 2020 Kowainik
License | MPL-2.0
Maintainer | Kowainik <xrom.xkov@gmail.com>
Safe Haskell | None
Language | Haskell2010

Stan.Pattern.Ast

Contents

- Type
- Helpers
- eDSL
- Pattern matching
- More low-level interface

Description

Patterns for AST and syntax tree nodes search.

Synopsis

- data PatternAst
  - = PatternAstConstant !Literal
  - | PatternAstName !NameMeta !PatternType
  - | PatternAstVarName !String
  - | PatternAstNode !(Set NodeAnnotation)
  - | PatternAstNodeExact !(Set NodeAnnotation) ![PatternAst]
  - | PatternAstAnything
  - | PatternAstOr !PatternAst !PatternAst
  - | PatternAstAnd !PatternAst !PatternAst
  - | PatternAstNeg !PatternAst
  - | PatternAstIdentifierDetailsDecl !DeclType
- data Literal
  - = ExactNum !Int
  - | ExactStr !ByteString
  - | PrefixStr !ByteString
  - | ContainStr !ByteString
  - | AnyLiteral
- namesToPatternAst :: NonEmpty (NameMeta, PatternType) -> PatternAst
- anyNamesToPatternAst :: NonEmpty NameMeta -> PatternAst
- app :: PatternAst -> PatternAst -> PatternAst
- opApp :: PatternAst -> PatternAst -> PatternAst -> PatternAst
- constructor :: PatternAst
- constructorNameIdentifier :: PatternAst
- dataDecl :: PatternAst
- fixity :: PatternAst
- fun :: PatternAst
- guardBranch :: PatternAst
- lazyField :: PatternAst
- range :: PatternAst -> PatternAst -> PatternAst
- rhs :: PatternAst
- tuple :: PatternAst
- typeSig :: PatternAst
- case' :: PatternAst
- lambdaCase :: PatternAst
- patternMatchBranch :: PatternAst
- patternMatchArrow :: PatternAst -> PatternAst
- patternMatch_ :: PatternAst -> PatternAst
- literalPat :: PatternAst
- wildPat :: PatternAst
- literalAnns :: NodeAnnotation

# Type

data PatternAst #

Query pattern used to search AST nodes in HIE AST. This data type tries to mirror HIE AST to each future matching, so it's quite low-level, but helper functions are provided.

Constructors

PatternAstConstant !Literal | Integer constant in code.
PatternAstName !NameMeta !PatternType | Name of a specific function, variable or data type.
PatternAstVarName !String | Variable name.
PatternAstNode | AST node with tags for current node and any children.
Fields !(Set NodeAnnotation) Set of context info (pairs of tags) | AST node with tags for current node and children patterns. This pattern should match the node exactly.
PatternAstNodeExact |
Fields !(Set NodeAnnotation) Set of context info (pairs of tags) ![PatternAst] Node children | AST wildcard, matches anything.
PatternAstAnything |
PatternAstOr !PatternAst !PatternAst | Choice between patterns. Should match either of them.
PatternAstAnd !PatternAst !PatternAst | Union of patterns. Should match both of them.
PatternAstNeg !PatternAst | Negation of pattern. Should match everything except this pattern.
PatternAstIdentifierDetailsDecl !DeclType | AST node with the specified Identifier details (only `DeclType`)

#### Instances

Instances details

Show PatternAst # |
Instance details Defined in Stan.Pattern.Ast Methods showsPrec :: Int -> PatternAst -> ShowS # show :: PatternAst -> String # showList :: [PatternAst] -> ShowS #
Eq PatternAst # |
Instance details Defined in Stan.Pattern.Ast Methods (==) :: PatternAst -> PatternAst -> Bool # (/=) :: PatternAst -> PatternAst -> Bool #
PatternBool PatternAst # |
Instance details Defined in Stan.Pattern.Ast Methods (?) :: PatternAst # neg :: PatternAst -> PatternAst # (|||) :: PatternAst -> PatternAst -> PatternAst # (&&&) :: PatternAst -> PatternAst -> PatternAst #

data Literal #

Constructors

ExactNum !Int |
ExactStr !ByteString |
PrefixStr !ByteString |
ContainStr !ByteString |
AnyLiteral |

#### Instances

Instances details

Show Literal # |
Instance details Defined in Stan.Pattern.Ast Methods showsPrec :: Int -> Literal -> ShowS # show :: Literal -> String # showList :: [Literal] -> ShowS #
Eq Literal # |
Instance details Defined in Stan.Pattern.Ast Methods (==) :: Literal -> Literal -> Bool # (/=) :: Literal -> Literal -> Bool #

# Helpers

namesToPatternAst :: NonEmpty (NameMeta, PatternType) -> PatternAst #

Function that creates `PatternAst` from the given non-empty list of pairs `NameMeta` and `PatternType`.

If the list contains only one `PatternType` then it is simple `PatternAstName`. Else it is `PatternAstOr` of all such `PatternAstName`s.

anyNamesToPatternAst :: NonEmpty NameMeta -> PatternAst #

Like `namesToPatternAst` but doesn't care about types.

# eDSL

app :: PatternAst -> PatternAst -> PatternAst #

`app f x` is a pattern for function application `f x`.

opApp :: PatternAst -> PatternAst -> PatternAst -> PatternAst #

`opApp x op y` is a pattern for operator application `x `op` y`.

constructor :: PatternAst #

Constructor of a plain data type or newtype. Children of node that matches this pattern are constructor fields.

constructorNameIdentifier :: PatternAst #

Constructor name Identifier info

dataDecl :: PatternAst #

`data` or `newtype` declaration.

fixity :: PatternAst #

Pattern for the top-level fixity declaration:

    infixr 7 ***, +++, ???

fun :: PatternAst #

Pattern for the function definition:

    foo x y = ...

guardBranch :: PatternAst #

Pattern for a single `guard` branch:

        | x < y = ...

lazyField :: PatternAst #

Lazy data type field. Comes in two shapes:

1. Record field, like: `foo :: Text`
2. Simple type: `Int`

range :: PatternAst -> PatternAst -> PatternAst #

`range a b` is a pattern for `[a .. b]`

rhs :: PatternAst #

Pattern for the right-hand-side. Usually an equality sign.

       foo = baz

tuple :: PatternAst #

Pattern for tuples:

- Type signatures: foo :: (Int, Int, Int, Int)
- Literals: (True, 0, [], Nothing)

typeSig :: PatternAst #

Pattern for the function type signature declaration:

    foo :: Some -> Type

# Pattern matching

case' :: PatternAst #

`case'` is a pattern for `case EXP of` expression (not considering branches).

lambdaCase :: PatternAst #

`lambdaCase` is a pattern for `case` expression (not considering branches).

patternMatchBranch :: PatternAst #

Pattern to represent one pattern matching branch.

patternMatchArrow :: PatternAst -> PatternAst #

Pattern to represent right side of the pattern matching, e.g. `-> "foo"`.

patternMatch_ :: PatternAst -> PatternAst #

Pattern to represent one pattern matching branch on `_`.

literalPat :: PatternAst #

Pattern for literals in pattern matching.

Note: presents on GHC >=8.10 only.

wildPat :: PatternAst #

Pattern for `_` in pattern matching.

Note: presents on GHC >=8.10 only.

# More low-level interface

literalAnns :: NodeAnnotation #

Annotations for constants: 0, "foo", etc.