1
1
mirror of https://github.com/github/semantic.git synced 2025-01-04 13:34:31 +03:00

Represent the tree-sitter parsers with a single constructor.

This commit is contained in:
Rob Rix 2017-05-19 15:39:03 -04:00
parent 1a80dcf2ce
commit d921337dae

View File

@ -31,16 +31,10 @@ data Parser term where
=> Parser (AST grammar) -- ^ A parser producing 'AST'. => Parser (AST grammar) -- ^ A parser producing 'AST'.
-> Assignment (Node grammar) (Term (Union fs) Location) -- ^ An assignment from 'AST' onto 'Term's. -> Assignment (Node grammar) (Term (Union fs) Location) -- ^ An assignment from 'AST' onto 'Term's.
-> Parser (Term (Union (Syntax.Error [Error grammar] ': fs)) Location) -- ^ A parser of 'Term's, weakened to allow for 'Syntax.Error' cases. -> Parser (Term (Union (Syntax.Error [Error grammar] ': fs)) Location) -- ^ A parser of 'Term's, weakened to allow for 'Syntax.Error' cases.
-- | A tree-sitter parser for 'C'. -- | A tree-sitter parser.
CParser :: Parser (SyntaxTerm Text DefaultFields) TreeSitterParser :: Language -> Ptr TS.Language -> Parser (SyntaxTerm Text DefaultFields)
-- | A tree-sitter parser for 'Go'.
GoParser :: Parser (SyntaxTerm Text DefaultFields)
-- | A parser for 'Markdown' using cmark. -- | A parser for 'Markdown' using cmark.
MarkdownParser :: Parser (SyntaxTerm Text DefaultFields) MarkdownParser :: Parser (SyntaxTerm Text DefaultFields)
-- | A tree-sitter parser for 'Ruby'.
RubyParser :: Parser (SyntaxTerm Text DefaultFields)
-- | A tree-sitter parser for 'TypeScript'.
TypeScriptParser :: Parser (SyntaxTerm Text DefaultFields)
-- | A parser which will parse any input 'Source' into a top-level 'Term' whose children are leaves consisting of the 'Source's lines. -- | A parser which will parse any input 'Source' into a top-level 'Term' whose children are leaves consisting of the 'Source's lines.
LineByLineParser :: Parser (SyntaxTerm Text DefaultFields) LineByLineParser :: Parser (SyntaxTerm Text DefaultFields)
@ -48,11 +42,11 @@ data Parser term where
parserForLanguage :: Maybe Language -> Parser (SyntaxTerm Text DefaultFields) parserForLanguage :: Maybe Language -> Parser (SyntaxTerm Text DefaultFields)
parserForLanguage Nothing = LineByLineParser parserForLanguage Nothing = LineByLineParser
parserForLanguage (Just language) = case language of parserForLanguage (Just language) = case language of
C -> CParser C -> TreeSitterParser C tree_sitter_c
TypeScript -> TypeScriptParser Go -> TreeSitterParser Go tree_sitter_go
Markdown -> MarkdownParser Markdown -> MarkdownParser
Ruby -> RubyParser Ruby -> TreeSitterParser Ruby tree_sitter_ruby
Language.Go -> GoParser TypeScript -> TreeSitterParser TypeScript tree_sitter_typescript
runParser :: Parser term -> Source -> IO term runParser :: Parser term -> Source -> IO term
runParser parser = case parser of runParser parser = case parser of
@ -61,11 +55,8 @@ runParser parser = case parser of
ast <- runParser parser source ast <- runParser parser source
let Result errors term = assign assignment source ast let Result errors term = assign assignment source ast
pure (maybe (cofree ((totalRange source :. totalSpan source :. Nil) :< inj (Syntax.Error errors))) (hoistCofree weaken) term) pure (maybe (cofree ((totalRange source :. totalSpan source :. Nil) :< inj (Syntax.Error errors))) (hoistCofree weaken) term)
CParser -> treeSitterParser C tree_sitter_c TreeSitterParser language tslanguage -> treeSitterParser language tslanguage
GoParser -> treeSitterParser Go tree_sitter_go
MarkdownParser -> cmarkParser MarkdownParser -> cmarkParser
RubyParser -> treeSitterParser Ruby tree_sitter_ruby
TypeScriptParser -> treeSitterParser TypeScript tree_sitter_typescript
LineByLineParser -> lineByLineParser LineByLineParser -> lineByLineParser
-- | A fallback parser that treats a file simply as rows of strings. -- | A fallback parser that treats a file simply as rows of strings.