3a_asm: FEATURE/FIX: proper line numbers on parsing error
authorBernhard Urban <lewurm@gmail.com>
Thu, 13 Jan 2011 23:28:10 +0000 (00:28 +0100)
committerBernhard Urban <lewurm@gmail.com>
Thu, 13 Jan 2011 23:28:10 +0000 (00:28 +0100)
3a_asm/DT.hs
3a_asm/DTFormat.hs
3a_asm/Main.hs
3a_asm/notes

index 519415981a0347bc5eb3b2d699ab070a804652c8..c80186ca399447d171e1eae70e7f17f99427f9ee 100644 (file)
@@ -17,12 +17,12 @@ import Control.Applicative hiding ((<|>))
 
 
 -- parsing --
-instruction :: Dict -> Parser Word32
-instruction dict = foldl1 (<|>) (fmap (\x -> try (x dict)) instructions) <* char '\n'
+instruction :: LineNo -> Dict -> Parser Word32
+instruction lno dict = foldl1 (<|>) (fmap (\x -> try (x dict)) instructions) <* char '\n' <?> (". something at line " ++ show lno ++ " is wrong (ignore line 1 hint above, but pay attention for possible line offset due to #include).")
 
 testins :: String -> IO ()
 testins input =
-       case (parse (instruction dict) "" (input++"\n")) of
+       case (parse (instruction dict) "" (input++"\n")) of
                Left err -> do { putStr "fail :/\n"; print err}
                Right x -> do { printf "0x%08X\n" x }
        where
index 17b2f014c8bc737dab587f75dc2bfbcabff301ce..fa1ddb1571e3bfacc8297155fc552c6485fa615f 100644 (file)
@@ -22,6 +22,7 @@ type Code = String
 type Label = String
 type Comment = String
 type Ascii = String
+type LineNo = Word32
 
 data DTF =
        DTF_Data Address Value Code Label Comment | -- 0;...
@@ -29,7 +30,7 @@ data DTF =
        DTF_Comment Comment | -- 2;...
        DTF_Label Label Comment Address | -- 3;...
        -- types for intern processing
-       DTF_InstrToParse Address ValueToParse Code Label Comment |
+       DTF_InstrToParse Address ValueToParse Code Label Comment LineNo |
        DTF_SectionToDet Address Value Code Label Comment |
        DTF_Org Address |
        DTF_Define Label Value Comment |
@@ -45,7 +46,7 @@ showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s)
 showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s)
 showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c)
 showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c)
-showsDTF (DTF_InstrToParse a v c l s) = (++) (printf "itp;%08x;%s;%s;%s;%s\n" a v c l s)
+showsDTF (DTF_InstrToParse a v c l s lno) = (++) (printf "itp;%08x;%s;%s;%s;%s@%d\n" a v c l s lno)
 showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08x;%s\n" l a c)
index b894fa16da6fca0ef049139d813cc024ea31fba2..c0de4f412093ebb19a73224ce78a5db14b19007a 100644 (file)
@@ -43,10 +43,10 @@ main = do
 
 parseInstr :: [DictElem] -> [DTF] -> [DTF]
 parseInstr _ [] = []
-parseInstr dict ((DTF_InstrToParse a instr c l s):xs) =
+parseInstr dict ((DTF_InstrToParse a instr c l s lno):xs) =
        (DTF_Instr a bytecode c l s):(parseInstr dict xs)
        where
-       bytecode = case (parse (instruction (a,dict)) "" (instr++"\n")) of
+       bytecode = case (parse (instruction lno (a,dict)) "" (instr++"\n")) of
                Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
                Right x -> x
 parseInstr dict (x:xs) = x:(parseInstr dict xs)
@@ -58,7 +58,7 @@ inc :: Counter -> Counter
 inc = ((+) 4)
 
 
-convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
+convertDTF :: [(LineNo,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
 convertDTF [] _ _ _ d = (d,[])
 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
        where
@@ -114,7 +114,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
                | state == InText = instrcnt + (4*rep)
                | otherwise = instrcnt
        ninstrcnt (DTF_Instr _ _ _ _ _) = inc instrcnt
-       ninstrcnt (DTF_InstrToParse _ _ _ _ _) = inc instrcnt
+       ninstrcnt (DTF_InstrToParse _ _ _ _ _ _) = inc instrcnt
        ninstrcnt _ = instrcnt
 
        nstate (DTF_State s) = s
@@ -122,7 +122,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
 
        ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
        ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
-       ndict (DTF_InstrToParse a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_InstrToParse a _ _ l _ _) = dict `add_elem` (l,a)
        ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
        ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
        ndict (DTF_Define l v _) = dict `add_elem` (l,v)
@@ -134,15 +134,15 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
                | otherwise = error $ "don't use .ascii in .text here: " ++ c
        ndict _ = dict
 
-       newdtf = case (parse (parseDTFLine dict) "" (str++"\n")) of
+       newdtf = case (parse (parseDTFLine lno dict) "" (str++"\n")) of
                Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
                Right (DTF_SectionToDet _ v c l s) ->
                        case state of
                                NoState -> error "missing .data or .text"
                                InData -> (DTF_Data datacnt v c l s)
                                InText -> (DTF_Instr instrcnt v c l s)
-               Right (DTF_InstrToParse _ v c l s) ->
-                       (DTF_InstrToParse instrcnt v c l s)
+               Right (DTF_InstrToParse _ v c l s ln) ->
+                       (DTF_InstrToParse instrcnt v c l s ln)
                Right y@(DTF_Org _) ->
                        case state of
                                NoState -> error "missing .data or .text"
@@ -156,14 +156,14 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
 
 testDTF :: String -> IO ()
 testDTF input =
-       case (parse (parseDTFLine dict) "" (input++"\n")) of
+       case (parse (parseDTFLine dict) "" (input++"\n")) of
                Left err -> do { putStr "failz ;(\n"; print err}
                Right x -> do { print x }
        where
        dict = [("lolz", 0x1337), ("rofl", 0xaaaa)]
 
-parseDTFLine :: [DictElem] -> Parser DTF
-parseDTFLine dict = foldl1 (<|>) (fmap (\x -> try (x dict)) lineFormats) <* char '\n'
+parseDTFLine :: LineNo -> [DictElem] -> Parser DTF
+parseDTFLine lno dict = foldl1 (<|>) (fmap (\x -> try (x lno dict)) lineFormats) <* char '\n'
 
 lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_ifill, lf_ascii, lf_comment, lf_toparse, lf_label, lf_nothing]
 
@@ -191,7 +191,7 @@ parseConst :: [DictElem] -> Parser Word32
 parseConst d = expr d
 
 -- teh pars0rs
-lf_data d = do
+lf_data d = do
        l <- try (parseLabel) <|> string ""
        skipMany space
        fill <- string ".fill "
@@ -207,18 +207,18 @@ lf_data d = do
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Fill r val (fill ++ (if repeat == "1" then "" else repeat) ++ code) l comment
 
-lf_ifill d = do
+lf_ifill lno d = do
        l <- try (parseLabel) <|> string ""
        skipMany space
        fill <- string ".ifill "
        code <- many1 $ noneOf "\n;"
-       let val = case parse (instruction (0,d)) "" (code++"\n") of
+       let val = case parse (instruction lno (0,d)) "" (code++"\n") of
                Right v -> v
                Left err -> error $ show err
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Fill 1 val (fill ++ code) l comment
 
-lf_ascii _ = do
+lf_ascii _ = do
        l <- try (parseLabel) <|> string ""
        skipMany space
        ascii <- string ".ascii "
@@ -228,46 +228,46 @@ lf_ascii _ = do
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Ascii str (ascii ++ "\"" ++ str ++ "\"") l comment
 
-lf_comment _ = do
+lf_comment _ = do
        comment <- parseComment
        return $ DTF_Comment comment
 
-lf_nothing _ = do
+lf_nothing _ = do
        wtf <- parseMySpaces
        return $ DTF_Comment wtf
 
-lf_label _ = do
+lf_label _ = do
        l <- parseLabel
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Label l comment 0
 
-lf_toparse _ = do
+lf_toparse lno _ = do
        l <- try(parseLabel) <|> string ""
        skipMany space
        code <- many1 $ noneOf "\n;"
        comment <- try(parseComment) <|> parseMySpaces
-       return $ DTF_InstrToParse 0 code code l comment
+       return $ DTF_InstrToParse 0 code code l comment lno
 
-lf_org d = do
+lf_org d = do
        skipMany space
        string ".org"
        val <- parseConst d
        parseMySpaces
        return $ DTF_Org val
 
-lf_sdata _ = do
+lf_sdata _ = do
        skipMany space
        string ".data"
        parseMySpaces
        return $ DTF_State InData
 
-lf_stext _ = do
+lf_stext _ = do
        skipMany space
        string ".text"
        parseMySpaces
        return $ DTF_State InText
 
-lf_define d = do
+lf_define d = do
        skipMany space
        string ".define"
        parseMySpaces
index 93707b504b875c470bcb9ff3a1d52b99d69528d6..07dfe0e23cc580b2f112d2bd57618c1cc7a57680 100644 (file)
@@ -4,7 +4,6 @@ BUGS:
 - parsers2: bei instr branches addresse durch 4 rechnen (ldi vorm brr -> RAGE)
 
 WUENSCHE:
-- vernuenftige zeilenangabe bei fehlermeldungen
 - eval: bitoperatoren