mirror of
https://github.com/github/semantic.git
synced 2024-12-29 01:42:43 +03:00
Rename Content to Source.
This commit is contained in:
parent
0ebf786747
commit
f97ed6cc37
@ -2,7 +2,7 @@
|
||||
module Data.Syntax.Assignment
|
||||
( Assignment
|
||||
, symbol
|
||||
, content
|
||||
, source
|
||||
, children
|
||||
, Rose(..)
|
||||
, RoseF(..)
|
||||
@ -28,7 +28,7 @@ type Assignment symbol = Freer (AssignmentF symbol)
|
||||
|
||||
data AssignmentF symbol a where
|
||||
Symbol :: symbol -> AssignmentF symbol ()
|
||||
Content :: AssignmentF symbol ByteString
|
||||
Source :: AssignmentF symbol ByteString
|
||||
Children :: Assignment symbol a -> AssignmentF symbol a
|
||||
Alt :: a -> a -> AssignmentF symbol a
|
||||
Empty :: AssignmentF symbol a
|
||||
@ -39,9 +39,9 @@ data AssignmentF symbol a where
|
||||
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
|
||||
content = Content `Then` return
|
||||
-- | A rule to produce a node’s source as a ByteString.
|
||||
source :: Assignment symbol ByteString
|
||||
source = Source `Then` return
|
||||
|
||||
-- | Match a node by applying an assignment to its children.
|
||||
children :: Assignment symbol a -> Assignment symbol a
|
||||
@ -52,8 +52,8 @@ children forEach = Children forEach `Then` return
|
||||
data Rose a = Rose { roseValue :: !a, roseChildren :: ![Rose a] }
|
||||
deriving (Eq, Functor, Show)
|
||||
|
||||
-- | A node in the input AST. We only concern ourselves with its symbol (considered as an element of 'grammar') and content.
|
||||
data Node grammar = Node { nodeSymbol :: grammar, nodeContent :: ByteString }
|
||||
-- | A node in the input AST. We only concern ourselves with its symbol (considered as an element of 'grammar') and source.
|
||||
data Node grammar = Node { nodeSymbol :: grammar, nodeSource :: ByteString }
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | An abstract syntax tree.
|
||||
@ -80,11 +80,11 @@ runAssignment = iterFreer (\ assignment yield nodes -> case (assignment, dropAno
|
||||
(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
|
||||
Symbol symbol -> guard (symbol == nodeSymbol) >> yield () nodes
|
||||
Content -> yield nodeContent rest
|
||||
Source -> yield nodeSource rest
|
||||
Children childAssignment -> assignAll childAssignment children >>= flip yield rest
|
||||
_ -> Error ["No rule to match " <> show node]
|
||||
(Symbol symbol, []) -> Error [ "Expected " <> show symbol <> " but got end of input." ]
|
||||
(Content, []) -> Error [ "Expected leaf node but got end of input." ]
|
||||
(Source, []) -> 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."])
|
||||
. fmap ((Result .) . flip (,))
|
||||
@ -100,7 +100,7 @@ instance Alternative (Assignment symbol) where
|
||||
instance Show symbol => Show1 (AssignmentF symbol) where
|
||||
liftShowsPrec sp sl d a = case a of
|
||||
Symbol s -> showsUnaryWith showsPrec "Symbol" d s . showChar ' ' . sp d ()
|
||||
Content -> showString "Content" . showChar ' ' . sp d ""
|
||||
Source -> showString "Source" . showChar ' ' . sp d ""
|
||||
Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a
|
||||
Alt a b -> showsBinaryWith sp sp "Alt" d a b
|
||||
Empty -> showString "Empty"
|
||||
|
@ -57,10 +57,10 @@ class' = term <$ symbol Class
|
||||
<*> children (Declaration.Class <$> constant <*> pure [] <*> many declaration)
|
||||
|
||||
constant :: Assignment Grammar (Term Syntax ())
|
||||
constant = term . Syntax.Identifier <$ symbol Constant <*> content
|
||||
constant = term . Syntax.Identifier <$ symbol Constant <*> source
|
||||
|
||||
identifier :: Assignment Grammar (Term Syntax ())
|
||||
identifier = term . Syntax.Identifier <$ symbol Identifier <*> content
|
||||
identifier = term . Syntax.Identifier <$ symbol Identifier <*> source
|
||||
|
||||
method :: Assignment Grammar (Term Syntax ())
|
||||
method = term <$ symbol Method
|
||||
@ -75,7 +75,7 @@ statement = exit Statement.Return Return
|
||||
where exit construct sym = term . construct <$ symbol sym <*> children (optional (symbol ArgumentList *> children expr))
|
||||
|
||||
comment :: Assignment Grammar (Term Syntax ())
|
||||
comment = term . Comment.Comment <$ symbol Comment <*> content
|
||||
comment = term . Comment.Comment <$ symbol Comment <*> source
|
||||
|
||||
if' :: Assignment Grammar (Term Syntax ())
|
||||
if' = go If
|
||||
@ -85,9 +85,9 @@ expr :: Assignment Grammar (Term Syntax ())
|
||||
expr = if' <|> literal
|
||||
|
||||
literal :: Assignment Grammar (Term Syntax ())
|
||||
literal = term Literal.true <$ symbol Language.Ruby.Syntax.True <* content
|
||||
<|> term Literal.false <$ symbol Language.Ruby.Syntax.False <* content
|
||||
<|> term . Literal.Integer <$ symbol Language.Ruby.Syntax.Integer <*> content
|
||||
literal = term Literal.true <$ symbol Language.Ruby.Syntax.True <* source
|
||||
<|> term Literal.false <$ symbol Language.Ruby.Syntax.False <* source
|
||||
<|> term . Literal.Integer <$ symbol Language.Ruby.Syntax.Integer <*> source
|
||||
|
||||
optional :: Assignment Grammar (Term Syntax ()) -> Assignment Grammar (Term Syntax ())
|
||||
optional a = a <|> pure (term Syntax.Empty)
|
||||
|
@ -30,12 +30,12 @@ spec = do
|
||||
it "does not advance past the current node" $
|
||||
fst <$> runAssignment (symbol Red) [ Rose (Node Red "hi") [] ] `shouldBe` Result [ Rose (Node Red "hi") [] ]
|
||||
|
||||
describe "content" $ do
|
||||
it "produces the node’s content" $
|
||||
snd <$> runAssignment content [ Rose (Node Red "hi") [] ] `shouldBe` Result "hi"
|
||||
describe "source" $ do
|
||||
it "produces the node’s source" $
|
||||
snd <$> runAssignment source [ Rose (Node Red "hi") [] ] `shouldBe` Result "hi"
|
||||
|
||||
it "advances past the current node" $
|
||||
fst <$> runAssignment content [ Rose (Node Red "hi") [] ] `shouldBe` Result []
|
||||
fst <$> runAssignment source [ Rose (Node Red "hi") [] ] `shouldBe` Result []
|
||||
|
||||
describe "children" $ do
|
||||
it "advances past the current node" $
|
||||
@ -49,15 +49,15 @@ spec = do
|
||||
|
||||
it "matches nested children" $ do
|
||||
runAssignment
|
||||
(symbol Red *> children (symbol Green *> children (symbol Blue *> content)))
|
||||
(symbol Red *> children (symbol Green *> children (symbol Blue *> source)))
|
||||
[ ast Red "" [ ast Green "" [ ast Blue "1" [] ] ] ]
|
||||
`shouldBe`
|
||||
Result ([], "1")
|
||||
|
||||
it "continues after children" $ do
|
||||
runAssignment
|
||||
(many (symbol Red *> children (symbol Green *> content)
|
||||
<|> symbol Blue *> content))
|
||||
(many (symbol Red *> children (symbol Green *> source)
|
||||
<|> symbol Blue *> source))
|
||||
[ ast Red "" [ ast Green "B" [] ]
|
||||
, ast Blue "C" [] ]
|
||||
`shouldBe`
|
||||
@ -65,7 +65,7 @@ spec = do
|
||||
|
||||
it "matches multiple nested children" $ do
|
||||
runAssignment
|
||||
(symbol Red *> children (many (symbol Green *> children (symbol Blue *> content))))
|
||||
(symbol Red *> children (many (symbol Green *> children (symbol Blue *> source))))
|
||||
[ 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 <$ symbol Red <*> content
|
||||
red = Out <$ symbol Red <*> source
|
||||
|
||||
green :: Assignment Grammar Out
|
||||
green = Out <$ symbol Green <*> content
|
||||
green = Out <$ symbol Green <*> source
|
||||
|
||||
blue :: Assignment Grammar Out
|
||||
blue = Out <$ symbol Blue <*> content
|
||||
blue = Out <$ symbol Blue <*> source
|
||||
|
Loading…
Reference in New Issue
Block a user