mirror of
https://github.com/github/semantic.git
synced 2025-01-07 07:58:12 +03:00
Use the usual Term synonym.
This commit is contained in:
parent
e4f6084183
commit
996fa27530
@ -10,11 +10,13 @@ import qualified Data.Syntax.Literal as Literal
|
|||||||
import qualified Data.Syntax.Statement as Statement
|
import qualified Data.Syntax.Statement as Statement
|
||||||
import Language.Haskell.TH
|
import Language.Haskell.TH
|
||||||
import Prologue hiding (optional)
|
import Prologue hiding (optional)
|
||||||
|
import Term
|
||||||
import Text.Parser.TreeSitter.Language
|
import Text.Parser.TreeSitter.Language
|
||||||
import Text.Parser.TreeSitter.Ruby
|
import Text.Parser.TreeSitter.Ruby
|
||||||
|
|
||||||
-- | The type of Ruby syntax.
|
-- | The type of Ruby syntax.
|
||||||
type Syntax =
|
type Syntax = Union Syntax'
|
||||||
|
type Syntax' =
|
||||||
'[Comment.Comment
|
'[Comment.Comment
|
||||||
, Declaration.Class
|
, Declaration.Class
|
||||||
, Declaration.Method
|
, Declaration.Method
|
||||||
@ -32,10 +34,7 @@ type Syntax =
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
-- | A program in some syntax functor, over which we can perform analyses.
|
term :: InUnion Syntax' f => () -> f (Term Syntax ()) -> Term Syntax ()
|
||||||
type Term = Cofree (Union Syntax) ()
|
|
||||||
|
|
||||||
term :: InUnion Syntax f => () -> f Term -> Term
|
|
||||||
term a f = cofree $ a :< inj f
|
term a f = cofree $ a :< inj f
|
||||||
|
|
||||||
|
|
||||||
@ -44,44 +43,44 @@ mkSymbolDatatype (mkName "Grammar") tree_sitter_ruby
|
|||||||
|
|
||||||
|
|
||||||
-- | Assignment from AST in Ruby’s grammar onto a program in Ruby’s syntax.
|
-- | Assignment from AST in Ruby’s grammar onto a program in Ruby’s syntax.
|
||||||
assignment :: Assignment Grammar [Term]
|
assignment :: Assignment Grammar [Term Syntax ()]
|
||||||
assignment = rule Program *> children (many declaration)
|
assignment = rule Program *> children (many declaration)
|
||||||
|
|
||||||
declaration :: Assignment Grammar Term
|
declaration :: Assignment Grammar (Term Syntax ())
|
||||||
declaration = comment <|> class' <|> method
|
declaration = comment <|> class' <|> method
|
||||||
|
|
||||||
class' :: Assignment Grammar Term
|
class' :: Assignment Grammar (Term Syntax ())
|
||||||
class' = term () <$ rule Class
|
class' = term () <$ rule Class
|
||||||
<*> children (Declaration.Class <$> constant <*> pure [] <*> many declaration)
|
<*> children (Declaration.Class <$> constant <*> pure [] <*> many declaration)
|
||||||
|
|
||||||
constant :: Assignment Grammar Term
|
constant :: Assignment Grammar (Term Syntax ())
|
||||||
constant = term () . Syntax.Identifier <$ rule Constant <*> content
|
constant = term () . Syntax.Identifier <$ rule Constant <*> content
|
||||||
|
|
||||||
identifier :: Assignment Grammar Term
|
identifier :: Assignment Grammar (Term Syntax ())
|
||||||
identifier = term () . Syntax.Identifier <$ rule Identifier <*> content
|
identifier = term () . Syntax.Identifier <$ rule Identifier <*> content
|
||||||
|
|
||||||
method :: Assignment Grammar Term
|
method :: Assignment Grammar (Term Syntax ())
|
||||||
method = term () <$ rule Method
|
method = term () <$ rule Method
|
||||||
<*> children (Declaration.Method <$> identifier <*> pure [] <*> (term () <$> many statement))
|
<*> 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)
|
statement = term () . Statement.Return <$ rule Return <*> children (optional expr)
|
||||||
<|> term () . Statement.Yield <$ rule Yield <*> children (optional expr)
|
<|> term () . Statement.Yield <$ rule Yield <*> children (optional expr)
|
||||||
<|> expr
|
<|> expr
|
||||||
|
|
||||||
comment :: Assignment Grammar Term
|
comment :: Assignment Grammar (Term Syntax ())
|
||||||
comment = term () . Comment.Comment <$ rule Comment <*> content
|
comment = term () . Comment.Comment <$ rule Comment <*> content
|
||||||
|
|
||||||
if' :: Assignment Grammar Term
|
if' :: Assignment Grammar (Term Syntax ())
|
||||||
if' = go If
|
if' = go If
|
||||||
where go symbol = term () <$ rule symbol <*> children (Statement.If <$> statement <*> (term () <$> many statement) <*> (go Elsif <|> term () <$ rule Else <*> children (many statement)))
|
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
|
expr = if' <|> literal
|
||||||
|
|
||||||
literal :: Assignment Grammar Term
|
literal :: Assignment Grammar (Term Syntax ())
|
||||||
literal = term () Literal.true <$ rule Language.Ruby.Syntax.True <* content
|
literal = term () Literal.true <$ rule Language.Ruby.Syntax.True <* content
|
||||||
<|> term () Literal.false <$ rule Language.Ruby.Syntax.False <* 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` [])
|
optional a = a <|> pure (() `term` [])
|
||||||
|
Loading…
Reference in New Issue
Block a user