-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add bracketing to lexer #68
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
dd87240
refactor: Add bracketing; kill withFC
croyzor 1ad6d60
kill withFC
croyzor a5b4df9
Revert pullAndJuxt changes
croyzor 39d101b
Drive-by: Update kind printing and remove Row kind
croyzor 81b49bc
test: Add extra let binding tests
croyzor a4c47dc
Update golden tests
croyzor 93e43c6
Fix warnings
croyzor 48612d4
Wee cleanup
croyzor e4842a6
drive-by: Fix karlheinz list application
croyzor 3c2be6f
fix: Don't allow trailing tokens when parsing between brackets
croyzor 16b1c85
Merge remote-tracking branch 'origin/main' into refactor/parser-wc
croyzor 4e9ac06
Propogate errors from parsers when going under brackets
croyzor a4997dc
Merge remote-tracking branch 'origin/main' into refactor/parser-wc
croyzor 06f671e
Fix vector FCs
croyzor 063634f
Merge remote-tracking branch 'origin/main' into refactor/parser-wc
croyzor c7eab1f
apply lints
croyzor 859a144
Review comments
croyzor 937ef01
Merge remote-tracking branch 'origin/main' into refactor/parser-wc
croyzor 4806481
[refactor] Combine `brackets` and `within` (#73)
acl-cqc 48e231b
Merge branch 'main' into refactor/parser-wc
croyzor 7df8f65
Merge branch 'main' into refactor/parser-wc
croyzor 44e1c3e
Merge opener and closer into openClose
croyzor ebf139f
Remove comments in BToken show instance
croyzor 625451d
Uncurry
croyzor 6cef983
run hlint
croyzor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module Brat.Lexer.Bracketed (BToken(..), brackets) where | ||
|
||
import Data.Bracket | ||
import Brat.Error (BracketErrMsg(..), Error(Err), ErrorMsg(..)) | ||
import Brat.FC | ||
import Brat.Lexer.Token | ||
|
||
import Data.List.NonEmpty (NonEmpty(..)) | ||
import Data.Bifunctor (first) | ||
import Text.Megaparsec (PosState(..), SourcePos(..), TraversableStream(..), VisualStream(..)) | ||
import Text.Megaparsec.Pos (mkPos) | ||
|
||
data OpenClose = Open BracketType | Close BracketType | ||
|
||
openClose :: Tok -> Maybe OpenClose | ||
openClose LParen = Just (Open Paren) | ||
openClose LSquare = Just (Open Square) | ||
openClose LBrace = Just (Open Brace) | ||
openClose RParen = Just (Close Paren) | ||
openClose RSquare = Just (Close Square) | ||
openClose RBrace = Just (Close Brace) | ||
openClose _ = Nothing | ||
|
||
-- Well bracketed tokens | ||
data BToken | ||
= Bracketed FC BracketType [BToken] | ||
| FlatTok Token | ||
deriving (Eq, Ord) | ||
|
||
btokLen :: BToken -> Int | ||
btokLen (FlatTok tok) = length (show tok) | ||
btokLen (Bracketed _ _ bs) = sum (btokLen <$> bs) + 2 | ||
|
||
instance Show BToken where | ||
show (FlatTok t) = show t | ||
show (Bracketed _ b ts) = showOpen b ++ concatMap show ts ++ showClose b | ||
|
||
instance VisualStream [BToken] where | ||
showTokens _ = concatMap show | ||
tokensLength _ = sum . fmap btokLen | ||
|
||
instance TraversableStream [BToken] where | ||
reachOffsetNoLine i pos = let fileName = sourceName (pstateSourcePos pos) | ||
(Pos line col, rest) = skipChars (i - pstateOffset pos + 1) (pstateInput pos) | ||
in pos | ||
{ pstateInput = rest | ||
, pstateOffset = max (pstateOffset pos) i | ||
, pstateSourcePos = SourcePos fileName (mkPos line) (mkPos col) | ||
} | ||
where | ||
skipChars :: Int -> [BToken] -> (Pos, [BToken]) | ||
skipChars 0 inp@(Bracketed fc _ _:_) = (start fc, inp) | ||
Check warning on line 52 in brat/Brat/Lexer/Bracketed.hs
|
||
skipChars 0 inp@(FlatTok t:_) = (start (fc t), inp) | ||
skipChars i ((Bracketed fc b bts):rest) = | ||
let Pos closeLine closeCol = end fc | ||
closeFC = FC (Pos closeLine (closeCol - 1)) (Pos closeLine closeCol) | ||
in skipChars (i - 1) (bts ++ [FlatTok (Token closeFC (closeTok b))] ++ rest) | ||
skipChars i (FlatTok t:rest) | ||
| i >= tokenLen t = skipChars (i - tokenLen t) rest | ||
| otherwise = (start (fc t), FlatTok t:rest) | ||
|
||
closeTok Paren = RParen | ||
closeTok Square = RSquare | ||
closeTok Brace = RBrace | ||
|
||
eofErr :: (FC, BracketType) -> Error | ||
eofErr (fc, b) = Err (Just fc) (BracketErr (EOFInBracket b)) | ||
|
||
openCloseMismatchErr :: (FC, BracketType) -> (FC, BracketType) -> Error | ||
openCloseMismatchErr open (fcClose, bClose) | ||
= Err (Just fcClose) (BracketErr (OpenCloseMismatch open bClose)) | ||
|
||
unexpectedCloseErr :: (FC, BracketType) -> Error | ||
unexpectedCloseErr (fc, b) = Err (Just fc) (BracketErr (UnexpectedClose b)) | ||
|
||
brackets :: [Token] -> Either Error [BToken] | ||
brackets ts = helper ts >>= \case | ||
(res, Nothing) -> pure res | ||
(_, Just (b, t:|_)) -> Left $ unexpectedCloseErr (fc t, b) | ||
where | ||
-- Given a list of tokens, either | ||
-- (success) return [BToken] consisting of the prefix of the input [Token] in which all opened brackets are closed, | ||
-- and any remaining [Token] beginning with a closer that does not match any opener in the input | ||
-- (either Nothing = no remaining tokens; or tokens with the BracketType that the first token closes) | ||
-- (failure) return an error, if a bracket opened in the input, is either not closed (EOF) or does not match the closer | ||
helper :: [Token] -> Either Error ([BToken], Maybe (BracketType, NonEmpty Token)) | ||
helper [] = pure ([], Nothing) | ||
helper (t:ts) = case openClose (_tok t) of | ||
Just (Open b) -> let openFC = fc t in helper ts >>= \case | ||
(_, Nothing) -> Left $ eofErr (fc t, b) | ||
(within, Just (b', r :| rs)) -> | ||
let closeFC = fc r | ||
enclosingFC = spanFC openFC closeFC | ||
in if b == b' | ||
then first (Bracketed enclosingFC b within:) <$> helper rs | ||
else Left $ openCloseMismatchErr (openFC, b) (closeFC, b') | ||
Just (Close b) -> pure ([], Just (b, t :| ts)) -- return closer for caller | ||
Nothing -> first (FlatTok t:) <$> helper ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty much an OpenCloseMismatch, without an opening (or where the opening is beginning of file) - consider combining the two, i.e.
OpenCloseMismatch (Maybe (FC, BracketType)) BracketType
. Or not, the conceptual similarity could just be confusing, up to you...