Haskell错误字符“ \ r”处的字符串/字符文字中的词法错误

 Happy的紫璐 发布于 2022-12-07 15:05

您好,我正在尝试编写一个将markdown转换为HTML的程序,我知道有Pandoc,但是我的项目是手动将其写出。我已经完成了,或者至少我认为我lexical error in string/character literal at character ' \r'愿意,但是我遇到了以下错误Haskell错误对于所指的内容我一无所知,指出它的任何帮助都会很棒。谢谢,所以更新了:我用其他符号更改了一些内容,现在得到的错误是hs.38:17: Not in scope 'str',当我处理该错误时,它一直反复指向第38行,在该行中我无法弄清问题所在,因为它忽略了相同的问题上一个函数中的确切内容

module Main
(
convertToHTML,
convertSpecialChar,
main
) where

import System.Environment (getArgs)
import System.IO
import Data.Char
import Data.List
import Data.List.Split

eof = ""

convertToHTML :: String -> String
convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---"
" x convertSpecialChar :: String -> String ConvertSpecialChar x = (convertLessThan $ convertAmpersand $ convertGreaterThan x)++eof where convertLessThan str = concat [if c =='<' then '<" else [c] | c <- str] convertAmpersand str = concat [if c == '&' then "&" else [c] | c <- str] convertGreaterThan str = concat [if c =='>' then ">" else [c] | c <- str] beginnings :: String -> String beginnings str = unwords $ map tag ch where tag x | isPrefixOf "**" x = "" ++ (tail $ tail x) | isPrefixOf "__" x = "" ++ (tail $ tail x) | isPrefixOf "_" x = "" ++ (tail x) | isPrefixOf "*" x = "" ++ (tail x) | isPrefixOf "^" x = "

" ++ (tail x) | isPrefixOf "---" x = replace "---" "


" | otherwise = x ch =splitOn " " str replace :: Eq a => [a] -> [a] -> [a] -> [a] replace old new x = intercalate new (splitOn old x) endings :: String -> String endings str = unwords $ map tag ch where tag x | isInfixOf "**" x = replace "**" "
" x | isInfixOf "__" x = replace "__" "
" x | isInfixOf "_" x = replace "_" "" x | isInfixOf "*" x = replace "*" "" x | isInfixOf "^" x = replace "^" "

" x | isInfixOf "---" x = replace "---" "
" x | otherwise = x ch = splitOn " " str headings str = unlines $ map heads (lines str) where heads x | isPrefixOf "######" x = "
" ++ (numTail 6 x) ++ "
" | isPrefixOf "#####" x = "
" ++ (numTail 5 x) ++ "
" | isPrefixOf "####" x = "

" ++ (numTail 4 x) ++ "

" | isPrefixOf "###" x = "

" ++ (numTail 3 x) ++ "

" | isPrefixOf "##" x = "

" ++ (tail $ tail x) ++ "

" | isPrefixOf "#" x = "

" ++ (tail x) ++ "

" | otherwise = x specialTags str = unlines $ map tags (lines str) where tags x | isPrefixOf "[code]" x = "
" ++ (numTail 6 x)
    | isSuffixOf "[code]" x = (numInit 6 x) ++ "
" | otherwise = x numTail :: Int -> String -> String numTail _ [] = [] numTail 1 str = tail str numTail x str = tail $ (numTail (x-1) str) numInit :: Int-> String -> String numInit _ [] = [] numInit 1 str = init str numInit x str = init $ (numInit (x-1) str) main = do args <- getArgs let (infile,outfile) = (\\(x:y:ys) -> (x,y)) args putStrLn $ "Input file: " ++ infile putStrLn $ "Output file: " ++ outfile contents <- readFile infile let contentlines = unlines $ tail $ lines contents let title = head $ lines contents let header = " " ++ "" ++ "" ++title++"" ++"" writeFile outfile $ convertToHTML $ header ++ convertSpecialChar contentlines

Stefan Holde.. 5

在第17行,您缺少双引号。我想replace "---"


" x应该读replace "---" "
" x

函数的整个声明convertToHTML将读取为

convertToHTML :: String -> String
convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---" "
" x

那为什么编译器会抱怨一个字符 '\r'呢?

编译Haskell模块的第一阶段是词法分析,其中将程序文本分解为标记,然后由解析器对其进行处理。在您的情况下,词法分析失败,因为假设新的字符串文字由开头" x。然后,它遇到了第17行的结尾,而文字却没有正确地以结束的双引号或任何表示字符串文字为多行字符串文字的指示正确终止。由于这是非法的词法语法,因此它抱怨遇到的行尾('\r')。

诚然,如果错误消息明确提到了一个非终止的字符串文字,那将会更有帮助。

无论如何,对于Haskell具有语法高亮显示支持的编辑器很可能早就暗​​示了这个问题。;)

缩进

然后,该变量str在您的本地定义中不在范围内的问题 ch是由于布局。确保ch缩进与的先前定义相同的级别tag。也就是说,代替

beginnings :: String -> String
beginnings str = unwords $ map tag ch
   where
   tag x
    | isPrefixOf "**" x = "" ++ (tail $ tail x)
    | isPrefixOf "__" x = "" ++ (tail $ tail x)
    | isPrefixOf  "_" x = "" ++ (tail x)
    | isPrefixOf "*" x = "" ++ (tail x)
    | isPrefixOf  "^" x = "

" ++ (tail x) | isPrefixOf "---" x = replace "---" "


" | otherwise = x ch =splitOn " " str

你应该写类似

beginnings :: String -> String
beginnings str = unwords $ map tag ch
  where
    tag x
      | isPrefixOf "**" x = "" ++ (tail $ tail x)
      | isPrefixOf "__" x = "" ++ (tail $ tail x)
      | isPrefixOf  "_" x = "" ++ (tail x)
      | isPrefixOf "*" x = "" ++ (tail x)
      | isPrefixOf  "^" x = "

" ++ (tail x) | isPrefixOf "---" x = replace "---" "


" | otherwise = x ch = splitOn " " str

请记住,在Haskell中,代码的布局很重要。

1 个回答
  • 在第17行,您缺少双引号。我想replace "---"<hr>" x应该读replace "---" "<hr>" x

    函数的整个声明convertToHTML将读取为

    convertToHTML :: String -> String
    convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---" "<hr>" x 
    

    那为什么编译器会抱怨一个字符 '\r'呢?

    编译Haskell模块的第一阶段是词法分析,其中将程序文本分解为标记,然后由解析器对其进行处理。在您的情况下,词法分析失败,因为假设新的字符串文字由开头" x。然后,它遇到了第17行的结尾,而文字却没有正确地以结束的双引号或任何表示字符串文字为多行字符串文字的指示正确终止。由于这是非法的词法语法,因此它抱怨遇到的行尾('\r')。

    诚然,如果错误消息明确提到了一个非终止的字符串文字,那将会更有帮助。

    无论如何,对于Haskell具有语法高亮显示支持的编辑器很可能早就暗​​示了这个问题。;)

    缩进

    然后,该变量str在您的本地定义中不在范围内的问题 ch是由于布局。确保ch缩进与的先前定义相同的级别tag。也就是说,代替

    beginnings :: String -> String
    beginnings str = unwords $ map tag ch
       where
       tag x
        | isPrefixOf "**" x = "<strong>" ++ (tail $ tail x)
        | isPrefixOf "__" x = "<strong>" ++ (tail $ tail x)
        | isPrefixOf  "_" x = "<em>" ++ (tail x)
        | isPrefixOf "*" x = "<em>" ++ (tail x)
        | isPrefixOf  "^" x = "<p>" ++ (tail x)
        | isPrefixOf "---" x = replace "---" "<hr>"
        | otherwise = x
    ch =splitOn " " str
    

    你应该写类似

    beginnings :: String -> String
    beginnings str = unwords $ map tag ch
      where
        tag x
          | isPrefixOf "**" x = "<strong>" ++ (tail $ tail x)
          | isPrefixOf "__" x = "<strong>" ++ (tail $ tail x)
          | isPrefixOf  "_" x = "<em>" ++ (tail x)
          | isPrefixOf "*" x = "<em>" ++ (tail x)
          | isPrefixOf  "^" x = "<p>" ++ (tail x)
          | isPrefixOf "---" x = replace "---" "<hr>"
          | otherwise = x
        ch = splitOn " " str
    

    请记住,在Haskell中,代码的布局很重要。

    2022-12-11 03:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有