mirror of
https://github.com/github/semantic.git
synced 2024-12-21 05:41:54 +03:00
Add remaining json syntax
This commit is contained in:
parent
381d5e741f
commit
e637d195b9
@ -19,7 +19,15 @@ import Prologue hiding (Location)
|
||||
|
||||
type Syntax =
|
||||
'[ Literal.Hash
|
||||
, Literal.KeyValue
|
||||
, Literal.Null
|
||||
, Literal.String
|
||||
, Literal.TextElement
|
||||
, Syntax.Error
|
||||
, Literal.Array
|
||||
, Literal.Boolean
|
||||
, Literal.Float
|
||||
, []
|
||||
]
|
||||
|
||||
type Term = Term.Term (Union Syntax) (Record Location)
|
||||
@ -35,12 +43,26 @@ parseError = makeTerm <$> symbol ParseError <*> (Syntax.Error [] <$ source)
|
||||
assignment :: Assignment
|
||||
assignment = object <|> array <|> parseError
|
||||
|
||||
value :: Assignment
|
||||
value = object <|> array <|> number <|> string <|> boolean <|> none
|
||||
|
||||
object :: Assignment
|
||||
object = makeTerm <$> symbol Object <*> children (Literal.Hash <$> many pairs)
|
||||
where pairs = makeTerm <$> symbol Pair <*> children (Literal.KeyValue <$> expression <*> expression)
|
||||
where pairs = makeTerm <$> symbol Pair <*> children (Literal.KeyValue <$> (number <|> string) <*> value)
|
||||
|
||||
array :: Assignment
|
||||
array = makeTerm <$> symbol Array <*> children (Literal.Array <$> many expression)
|
||||
array = makeTerm <$> symbol Array <*> children (Literal.Array <$> many value)
|
||||
|
||||
number :: Assignment
|
||||
number = makeTerm <$> symbol Number <*> (Literal.Float <$> source)
|
||||
|
||||
string :: Assignment
|
||||
string = makeTerm <$> symbol String <*> (Literal.TextElement <$> source)
|
||||
|
||||
boolean :: Assignment
|
||||
boolean = makeTerm <$> symbol Grammar.True <*> (Literal.true <$ source)
|
||||
<|> makeTerm <$> symbol Grammar.False <*> (Literal.false <$ source)
|
||||
|
||||
none :: Assignment
|
||||
none = makeTerm <$> symbol Null <*> (Literal.Null <$ source)
|
||||
|
||||
|
@ -5,6 +5,7 @@ module Parser
|
||||
-- Syntax parsers
|
||||
, parserForLanguage
|
||||
-- À la carte parsers
|
||||
, jsonParser
|
||||
, markdownParser
|
||||
, pythonParser
|
||||
, rubyParser
|
||||
@ -20,6 +21,7 @@ import Data.Union
|
||||
import Info hiding (Empty, Go)
|
||||
import Language
|
||||
import Language.Markdown
|
||||
import qualified Language.JSON.Syntax as JSON
|
||||
import qualified Language.Markdown.Syntax as Markdown
|
||||
import qualified Language.Python.Syntax as Python
|
||||
import qualified Language.Ruby.Syntax as Ruby
|
||||
@ -71,6 +73,9 @@ rubyParser = AssignmentParser (ASTParser tree_sitter_ruby) headF Ruby.assignment
|
||||
pythonParser :: Parser Python.Term
|
||||
pythonParser = AssignmentParser (ASTParser tree_sitter_python) headF Python.assignment
|
||||
|
||||
jsonParser :: Parser JSON.Term
|
||||
jsonParser = AssignmentParser (ASTParser tree_sitter_json) headF JSON.assignment
|
||||
|
||||
markdownParser :: Parser Markdown.Term
|
||||
markdownParser = AssignmentParser MarkdownParser (\ (node@Node{..} :< _) -> node { nodeSymbol = toGrammar nodeSymbol }) Markdown.assignment
|
||||
|
||||
|
@ -70,12 +70,15 @@ diffBlobPair renderer blobs = case (renderer, effectiveLanguage) of
|
||||
(ToCDiffRenderer, _) -> run (\ blobSource -> parse syntaxParser blobSource >>= decorate (syntaxDeclarationAlgebra blobSource)) diffTerms (renderToCDiff blobs)
|
||||
(JSONDiffRenderer, Just Language.Markdown) -> run (parse markdownParser) diffLinearly (renderJSONDiff blobs)
|
||||
(JSONDiffRenderer, Just Language.Python) -> run (parse pythonParser) diffLinearly (renderJSONDiff blobs)
|
||||
(JSONDiffRenderer, Just Language.JSON) -> run (parse jsonParser) diffLinearly (renderJSONDiff blobs)
|
||||
(JSONDiffRenderer, _) -> run (decorate identifierAlgebra <=< parse syntaxParser) diffTerms (renderJSONDiff blobs)
|
||||
(PatchDiffRenderer, Just Language.Markdown) -> run (parse markdownParser) diffLinearly (renderPatch blobs)
|
||||
(PatchDiffRenderer, Just Language.Python) -> run (parse pythonParser) diffLinearly (renderPatch blobs)
|
||||
(PatchDiffRenderer, Just Language.JSON) -> run (parse jsonParser) diffLinearly (renderPatch blobs)
|
||||
(PatchDiffRenderer, _) -> run (parse syntaxParser) diffTerms (renderPatch blobs)
|
||||
(SExpressionDiffRenderer, Just Language.Markdown) -> run (decorate (ConstructorLabel . constructorLabel) <=< parse markdownParser) diffLinearly (renderSExpressionDiff . mapAnnotations keepConstructorLabel)
|
||||
(SExpressionDiffRenderer, Just Language.Python) -> run (decorate (ConstructorLabel . constructorLabel) <=< parse pythonParser) diffLinearly (renderSExpressionDiff . mapAnnotations keepConstructorLabel)
|
||||
(SExpressionDiffRenderer, Just Language.JSON) -> run (decorate (ConstructorLabel . constructorLabel) <=< parse jsonParser) diffLinearly (renderSExpressionDiff . mapAnnotations keepConstructorLabel)
|
||||
(SExpressionDiffRenderer, _) -> run (parse syntaxParser) diffTerms (renderSExpressionDiff . mapAnnotations keepCategory)
|
||||
(IdentityDiffRenderer, _) -> run (\ blobSource -> parse syntaxParser blobSource >>= decorate (syntaxDeclarationAlgebra blobSource)) diffTerms Just
|
||||
where effectiveLanguage = runBothWith (<|>) (blobLanguage <$> blobs)
|
||||
|
Loading…
Reference in New Issue
Block a user