Skip to content

Commit

Permalink
Make diagnostics parsing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
alanz committed Feb 16, 2021
1 parent 0b09d4d commit 484decb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
29 changes: 24 additions & 5 deletions semantic-ast/src/AST/Unmarshal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import TreeSitter.Language as TS
import TreeSitter.Node as TS
import TreeSitter.Parser as TS
import TreeSitter.Tree as TS
import Control.Applicative ((<|>))

-- Parse source code and produce AST
parseByteString :: (Unmarshal t, UnmarshalAnn a) => Ptr TS.Language -> ByteString -> IO (Either String (UnmarshalDiagnostics, (t a)))
Expand Down Expand Up @@ -495,6 +496,7 @@ posdiagnostics = do
pure xs

-- "((16,19) (ERROR)),((24,24) (MISSING \".\")),")]
-- ((15,15) (MISSING {_raw_atom}))
posdiagnostic :: Attoparsec.Parser ((Int,Int), TSDiagnostic)
posdiagnostic = do
void $ char '('
Expand Down Expand Up @@ -530,17 +532,34 @@ pmissing :: Attoparsec.Parser TSDiagnostic
pmissing = do
void $ string "MISSING"
void $ char ' '
void $ char '"'
s <- takeWhile1 (/= '"')
void $ char '"'
s <- pquoted
pure (TSDMissing s)

punexpected :: Attoparsec.Parser TSDiagnostic
punexpected = do
void $ string "UNEXPECTED"
void $ char ' '
s <- pquoted
pure (TSDUnexpected s)

pquoted :: Attoparsec.Parser Text.Text
pquoted =
pquotedString
<|> pbracedString

pquotedString :: Attoparsec.Parser Text.Text
pquotedString = do
void $ char '"'
s <- takeWhile1 (/= '"')
void $ char '"'
pure (TSDUnexpected s)

pure s

-- TODO: this should probably be an SEXP parser. But we wrap the
-- missng part in braces in the tree-sitter C part, so should work
-- unless there is a '}' in the quoted fragment.
pbracedString :: Attoparsec.Parser Text.Text
pbracedString = do
void $ char '{'
s <- takeWhile1 (/= '}')
void $ char '}'
pure s
5 changes: 3 additions & 2 deletions semantic/src/Parsing/TreeSitter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module Parsing.TreeSitter
, parseToPreciseAST
) where

import Control.Carrier.Reader
import Control.Carrier.State.Strict
-- import Control.Carrier.Reader
import Control.Exception as Exc
import Control.Monad.IO.Class
import Foreign
Expand Down Expand Up @@ -51,7 +52,7 @@ parseToPreciseAST
parseToPreciseAST parseTimeout unmarshalTimeout language blob = runParse parseTimeout language blob $ \ rootPtr ->
withTimeout $
TS.withCursor (castPtr rootPtr) $ \ cursor ->
runReader (TS.UnmarshalState (Source.bytes (blobSource blob)) cursor) (liftIO (peek rootPtr) >>= TS.unmarshalNode)
runState (TS.UnmarshalState (Source.bytes (blobSource blob)) cursor mempty) (liftIO (peek rootPtr) >>= TS.unmarshalNode)
`Exc.catch` (Exc.throw . UnmarshalFailure . TS.getUnmarshalError)
where
withTimeout :: IO a -> IO a
Expand Down

0 comments on commit 484decb

Please sign in to comment.