Switch Span to offsets

This commit is contained in:
Joscha 2020-04-05 13:44:50 +00:00
parent b85ff076ba
commit 6e1f3e213c

View file

@ -22,13 +22,10 @@ import Text.Megaparsec.Char.Lexer hiding (space)
import Mima.Asm.Phase1.Types import Mima.Asm.Phase1.Types
import qualified Mima.Vm.Instruction as Vm import qualified Mima.Vm.Instruction as Vm
data Span = Span SourcePos SourcePos data Span = Span Int Int
instance Show Span where instance Show Span where
show (Span start end) = "<" ++ showPos start ++ "-" ++ showPos end ++ ">" show (Span start end) = "<" ++ show start ++ "-" ++ show end ++ ">"
where
showPos pos =
show (unPos $ sourceLine pos) ++ ":" ++ show (unPos $ sourceColumn pos)
type Parser = Parsec Void T.Text type Parser = Parsec Void T.Text
@ -40,9 +37,9 @@ inlineSpace1 = void $ takeWhile1P (Just "space (no newline)") (\x -> isSpace x &
withSpan :: Parser a -> Parser (Span, a) withSpan :: Parser a -> Parser (Span, a)
withSpan f = do withSpan f = do
start <- getSourcePos start <- getOffset
result <- f result <- f
stop <- getSourcePos stop <- getOffset
pure (Span start stop, result) pure (Span start stop, result)
name :: Parser (Name Span) name :: Parser (Name Span)
@ -104,17 +101,17 @@ instruction :: Parser (Instruction Span)
instruction = small <|> large instruction = small <|> large
where where
small = do small = do
start <- getSourcePos start <- getOffset
so <- smallOpcode so <- smallOpcode
inlineSpace1 inlineSpace1
loc <- location loc <- location
stop <- getSourcePos stop <- getOffset
pure $ SmallInstruction (Span start stop) so loc pure $ SmallInstruction (Span start stop) so loc
large = do large = do
start <- getSourcePos start <- getOffset
lo <- largeOpcode lo <- largeOpcode
sv <- optionalAwareArgument lo sv <- optionalAwareArgument lo
stop <- getSourcePos stop <- getOffset
pure $ LargeInstruction (Span start stop) lo sv pure $ LargeInstruction (Span start stop) lo sv
optionalAwareArgument (LargeOpcode _ code) optionalAwareArgument (LargeOpcode _ code)
| Vm.argumentIsOptional code = optional (inlineSpace1 *> smallValue <?> "argument") | Vm.argumentIsOptional code = optional (inlineSpace1 *> smallValue <?> "argument")