mirror of
https://github.com/github/semantic.git
synced 2024-12-01 00:33:59 +03:00
Rename rule to symbol.
This commit is contained in:
parent
996fa27530
commit
c589e229ee
@ -1,7 +1,7 @@
|
||||
{-# LANGUAGE GADTs, TypeFamilies #-}
|
||||
module Data.Syntax.Assignment
|
||||
( Assignment
|
||||
, rule
|
||||
, symbol
|
||||
, content
|
||||
, children
|
||||
, Rose(..)
|
||||
@ -27,7 +27,7 @@ import Text.Show hiding (show)
|
||||
type Assignment symbol = Freer (AssignmentF symbol)
|
||||
|
||||
data AssignmentF symbol a where
|
||||
Rule :: symbol -> AssignmentF symbol ()
|
||||
Symbol :: symbol -> AssignmentF symbol ()
|
||||
Content :: AssignmentF symbol ByteString
|
||||
Children :: Assignment symbol a -> AssignmentF symbol a
|
||||
Alt :: a -> a -> AssignmentF symbol a
|
||||
@ -36,8 +36,8 @@ data AssignmentF symbol a where
|
||||
-- | Zero-width match of a node with the given symbol.
|
||||
--
|
||||
-- Since this is zero-width, care must be taken not to repeat it without chaining on other rules. I.e. 'many (rule A *> b)' is fine, but 'many (rule A)' is not.
|
||||
rule :: symbol -> Assignment symbol ()
|
||||
rule symbol = Rule symbol `Then` return
|
||||
symbol :: symbol -> Assignment symbol ()
|
||||
symbol s = Symbol s `Then` return
|
||||
|
||||
-- | A rule to produce a node’s content as a ByteString.
|
||||
content :: Assignment symbol ByteString
|
||||
@ -77,13 +77,13 @@ assignAll assignment nodes = case runAssignment assignment nodes of
|
||||
runAssignment :: (Symbol grammar, Eq grammar, Show grammar) => Assignment grammar a -> [AST grammar] -> Result ([AST grammar], a)
|
||||
runAssignment = iterFreer (\ assignment yield nodes -> case (assignment, dropAnonymous nodes) of
|
||||
-- Nullability: some rules, e.g. 'pure a' and 'many a', should match at the end of input. Either side of an alternation may be nullable, ergo Alt can match at the end of input.
|
||||
(Alt a b, nodes) -> yield a nodes <|> yield b nodes -- FIXME: Rule `Alt` Rule `Alt` Rule is inefficient, should build and match against an IntMap instead.
|
||||
(Alt a b, nodes) -> yield a nodes <|> yield b nodes -- FIXME: Symbol `Alt` Symbol `Alt` Symbol is inefficient, should build and match against an IntMap instead.
|
||||
(assignment, node@(Rose Node{..} children) : rest) -> case assignment of
|
||||
Rule symbol -> guard (symbol == nodeSymbol) >> yield () nodes
|
||||
Symbol symbol -> guard (symbol == nodeSymbol) >> yield () nodes
|
||||
Content -> yield nodeContent rest
|
||||
Children childAssignment -> assignAll childAssignment children >>= flip yield rest
|
||||
_ -> Error ["No rule to match " <> show node]
|
||||
(Rule symbol, []) -> Error [ "Expected " <> show symbol <> " but got end of input." ]
|
||||
(Symbol symbol, []) -> Error [ "Expected " <> show symbol <> " but got end of input." ]
|
||||
(Content, []) -> Error [ "Expected leaf node but got end of input." ]
|
||||
(Children _, []) -> Error [ "Expected branch node but got end of input." ]
|
||||
_ -> Error ["No rule to match at end of input."])
|
||||
@ -99,7 +99,7 @@ instance Alternative (Assignment symbol) where
|
||||
|
||||
instance Show symbol => Show1 (AssignmentF symbol) where
|
||||
liftShowsPrec sp sl d a = case a of
|
||||
Rule s -> showsUnaryWith showsPrec "Rule" d s . showChar ' ' . sp d ()
|
||||
Symbol s -> showsUnaryWith showsPrec "Symbol" d s . showChar ' ' . sp d ()
|
||||
Content -> showString "Content" . showChar ' ' . sp d ""
|
||||
Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a
|
||||
Alt a b -> showsBinaryWith sp sp "Alt" d a b
|
||||
|
@ -44,43 +44,43 @@ mkSymbolDatatype (mkName "Grammar") tree_sitter_ruby
|
||||
|
||||
-- | Assignment from AST in Ruby’s grammar onto a program in Ruby’s syntax.
|
||||
assignment :: Assignment Grammar [Term Syntax ()]
|
||||
assignment = rule Program *> children (many declaration)
|
||||
assignment = symbol Program *> children (many declaration)
|
||||
|
||||
declaration :: Assignment Grammar (Term Syntax ())
|
||||
declaration = comment <|> class' <|> method
|
||||
|
||||
class' :: Assignment Grammar (Term Syntax ())
|
||||
class' = term () <$ rule Class
|
||||
class' = term () <$ symbol Class
|
||||
<*> children (Declaration.Class <$> constant <*> pure [] <*> many declaration)
|
||||
|
||||
constant :: Assignment Grammar (Term Syntax ())
|
||||
constant = term () . Syntax.Identifier <$ rule Constant <*> content
|
||||
constant = term () . Syntax.Identifier <$ symbol Constant <*> content
|
||||
|
||||
identifier :: Assignment Grammar (Term Syntax ())
|
||||
identifier = term () . Syntax.Identifier <$ rule Identifier <*> content
|
||||
identifier = term () . Syntax.Identifier <$ symbol Identifier <*> content
|
||||
|
||||
method :: Assignment Grammar (Term Syntax ())
|
||||
method = term () <$ rule Method
|
||||
method = term () <$ symbol Method
|
||||
<*> children (Declaration.Method <$> identifier <*> pure [] <*> (term () <$> many statement))
|
||||
|
||||
statement :: Assignment Grammar (Term Syntax ())
|
||||
statement = term () . Statement.Return <$ rule Return <*> children (optional expr)
|
||||
<|> term () . Statement.Yield <$ rule Yield <*> children (optional expr)
|
||||
statement = term () . Statement.Return <$ symbol Return <*> children (optional expr)
|
||||
<|> term () . Statement.Yield <$ symbol Yield <*> children (optional expr)
|
||||
<|> expr
|
||||
|
||||
comment :: Assignment Grammar (Term Syntax ())
|
||||
comment = term () . Comment.Comment <$ rule Comment <*> content
|
||||
comment = term () . Comment.Comment <$ symbol Comment <*> content
|
||||
|
||||
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)))
|
||||
where go s = term () <$ symbol s <*> children (Statement.If <$> statement <*> (term () <$> many statement) <*> (go Elsif <|> term () <$ symbol Else <*> children (many statement)))
|
||||
|
||||
expr :: Assignment Grammar (Term Syntax ())
|
||||
expr = if' <|> literal
|
||||
|
||||
literal :: Assignment Grammar (Term Syntax ())
|
||||
literal = term () Literal.true <$ rule Language.Ruby.Syntax.True <* content
|
||||
<|> term () Literal.false <$ rule Language.Ruby.Syntax.False <* content
|
||||
literal = term () Literal.true <$ symbol Language.Ruby.Syntax.True <* content
|
||||
<|> term () Literal.false <$ symbol Language.Ruby.Syntax.False <* content
|
||||
|
||||
optional :: Assignment Grammar (Term Syntax ()) -> Assignment Grammar (Term Syntax ())
|
||||
optional a = a <|> pure (() `term` [])
|
||||
|
@ -23,12 +23,12 @@ spec = do
|
||||
it "matches one-or-more repetitions against one or more input nodes" $
|
||||
runAssignment (some red) [ast Red "hello" []] `shouldBe` Result ([], [Out "hello"])
|
||||
|
||||
describe "rule" $ do
|
||||
describe "symbol" $ do
|
||||
it "matches nodes with the same symbol" $
|
||||
runAssignment red [ast Red "hello" []] `shouldBe` Result ([], Out "hello")
|
||||
|
||||
it "does not advance past the current node" $
|
||||
fst <$> runAssignment (rule Red) [ Rose (Node Red "hi") [] ] `shouldBe` Result [ Rose (Node Red "hi") [] ]
|
||||
fst <$> runAssignment (symbol Red) [ Rose (Node Red "hi") [] ] `shouldBe` Result [ Rose (Node Red "hi") [] ]
|
||||
|
||||
describe "content" $ do
|
||||
it "produces the node’s content" $
|
||||
@ -49,15 +49,15 @@ spec = do
|
||||
|
||||
it "matches nested children" $ do
|
||||
runAssignment
|
||||
(rule Red *> children (rule Green *> children (rule Blue *> content)))
|
||||
(symbol Red *> children (symbol Green *> children (symbol Blue *> content)))
|
||||
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ] ] ]
|
||||
`shouldBe`
|
||||
Result ([], "1")
|
||||
|
||||
it "continues after children" $ do
|
||||
runAssignment
|
||||
(many (rule Red *> children (rule Green *> content)
|
||||
<|> rule Blue *> content))
|
||||
(many (symbol Red *> children (symbol Green *> content)
|
||||
<|> symbol Blue *> content))
|
||||
[ ast Red "" [ ast Green "B" [] ]
|
||||
, ast Blue "C" [] ]
|
||||
`shouldBe`
|
||||
@ -65,7 +65,7 @@ spec = do
|
||||
|
||||
it "matches multiple nested children" $ do
|
||||
runAssignment
|
||||
(rule Red *> children (many (rule Green *> children (rule Blue *> content))))
|
||||
(symbol Red *> children (many (symbol Green *> children (symbol Blue *> content))))
|
||||
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ]
|
||||
, ast Green "" [ ast Blue "2" [] ] ] ]
|
||||
`shouldBe`
|
||||
@ -84,10 +84,10 @@ data Out = Out ByteString
|
||||
deriving (Eq, Show)
|
||||
|
||||
red :: Assignment Grammar Out
|
||||
red = Out <$ rule Red <*> content
|
||||
red = Out <$ symbol Red <*> content
|
||||
|
||||
green :: Assignment Grammar Out
|
||||
green = Out <$ rule Green <*> content
|
||||
green = Out <$ symbol Green <*> content
|
||||
|
||||
blue :: Assignment Grammar Out
|
||||
blue = Out <$ rule Blue <*> content
|
||||
blue = Out <$ symbol Blue <*> content
|
||||
|
Loading…
Reference in New Issue
Block a user