1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 13:02:37 +03:00

Use the usual Term synonym.

This commit is contained in:
Rob Rix 2017-04-19 13:06:06 -04:00
parent e4f6084183
commit 996fa27530

View File

@ -10,11 +10,13 @@ import qualified Data.Syntax.Literal as Literal
import qualified Data.Syntax.Statement as Statement
import Language.Haskell.TH
import Prologue hiding (optional)
import Term
import Text.Parser.TreeSitter.Language
import Text.Parser.TreeSitter.Ruby
-- | The type of Ruby syntax.
type Syntax =
type Syntax = Union Syntax'
type Syntax' =
'[Comment.Comment
, Declaration.Class
, Declaration.Method
@ -32,10 +34,7 @@ type Syntax =
]
-- | A program in some syntax functor, over which we can perform analyses.
type Term = Cofree (Union Syntax) ()
term :: InUnion Syntax f => () -> f Term -> Term
term :: InUnion Syntax' f => () -> f (Term Syntax ()) -> Term Syntax ()
term a f = cofree $ a :< inj f
@ -44,44 +43,44 @@ mkSymbolDatatype (mkName "Grammar") tree_sitter_ruby
-- | Assignment from AST in Rubys grammar onto a program in Rubys syntax.
assignment :: Assignment Grammar [Term]
assignment :: Assignment Grammar [Term Syntax ()]
assignment = rule Program *> children (many declaration)
declaration :: Assignment Grammar Term
declaration :: Assignment Grammar (Term Syntax ())
declaration = comment <|> class' <|> method
class' :: Assignment Grammar Term
class' :: Assignment Grammar (Term Syntax ())
class' = term () <$ rule Class
<*> children (Declaration.Class <$> constant <*> pure [] <*> many declaration)
constant :: Assignment Grammar Term
constant :: Assignment Grammar (Term Syntax ())
constant = term () . Syntax.Identifier <$ rule Constant <*> content
identifier :: Assignment Grammar Term
identifier :: Assignment Grammar (Term Syntax ())
identifier = term () . Syntax.Identifier <$ rule Identifier <*> content
method :: Assignment Grammar Term
method :: Assignment Grammar (Term Syntax ())
method = term () <$ rule Method
<*> children (Declaration.Method <$> identifier <*> pure [] <*> (term () <$> many statement))
statement :: Assignment Grammar Term
statement :: Assignment Grammar (Term Syntax ())
statement = term () . Statement.Return <$ rule Return <*> children (optional expr)
<|> term () . Statement.Yield <$ rule Yield <*> children (optional expr)
<|> expr
comment :: Assignment Grammar Term
comment :: Assignment Grammar (Term Syntax ())
comment = term () . Comment.Comment <$ rule Comment <*> content
if' :: Assignment Grammar Term
if' :: Assignment Grammar (Term Syntax ())
if' = go If
where go symbol = term () <$ rule symbol <*> children (Statement.If <$> statement <*> (term () <$> many statement) <*> (go Elsif <|> term () <$ rule Else <*> children (many statement)))
expr :: Assignment Grammar Term
expr :: Assignment Grammar (Term Syntax ())
expr = if' <|> literal
literal :: Assignment Grammar Term
literal :: Assignment Grammar (Term Syntax ())
literal = term () Literal.true <$ rule Language.Ruby.Syntax.True <* content
<|> term () Literal.false <$ rule Language.Ruby.Syntax.False <* content
optional :: Assignment Grammar Term -> Assignment Grammar Term
optional :: Assignment Grammar (Term Syntax ()) -> Assignment Grammar (Term Syntax ())
optional a = a <|> pure (() `term` [])