Capstone B: errors a user can read
An error a user can read
The lexer now records the column of every token and never calls error: an unknown character is a token of its own, and the parser reports it with the position it was found at. The program takes an expression as its argument, prints the value or the error, and leaves status 2 for a bad input so a script can tell the two apart.
data Located = Located Int Token
parseFactor (Located position token : _) =
Left ("unexpected " ++ describe token ++ " at column " ++ show position)
The column is one-based and counts characters from the start of the expression, which is what a person counting in an editor expects. The describe function turns a token into the word the message uses, so TBad '@' reads as character '@'.
The program
module Main (main) where
import Data.Char (isDigit)
import System.Environment (getArgs)
import System.Exit (ExitCode (ExitFailure), exitWith)
import System.IO (hPutStrLn, stderr)
data Token
= TNum Int
| TPlus
| TMinus
| TStar
| TSlash
| TLParen
| TRParen
| TBad Char
deriving (Show, Eq)
data Located = Located Int Token
deriving (Show)
data Expr
= Num Int
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
deriving (Show, Eq)
lexer :: String -> [Located]
lexer = go 1
where
go _ [] = []
go n (c : cs)
| c == ' ' = go (n + 1) cs
| c `elem` "+-*/()" = Located n (symbol c) : go (n + 1) cs
| isDigit c =
let (digits, rest) = span isDigit (c : cs)
in Located n (TNum (read digits)) : go (n + length digits) rest
| otherwise = Located n (TBad c) : go (n + 1) cs
symbol '+' = TPlus
symbol '-' = TMinus
symbol '*' = TStar
symbol '/' = TSlash
symbol '(' = TLParen
symbol ')' = TRParen
symbol other = TBad other
parseExpr :: [Located] -> Either String (Expr, [Located])
parseExpr tokens = do
(left, rest) <- parseTerm tokens
restExpr left rest
where
restExpr left (Located _ TPlus : rest) = do
(right, rest') <- parseTerm rest
restExpr (Add left right) rest'
restExpr left (Located _ TMinus : rest) = do
(right, rest') <- parseTerm rest
restExpr (Sub left right) rest'
restExpr left rest = Right (left, rest)
parseTerm :: [Located] -> Either String (Expr, [Located])
parseTerm tokens = do
(left, rest) <- parseFactor tokens
restTerm left rest
where
restTerm left (Located _ TStar : rest) = do
(right, rest') <- parseFactor rest
restTerm (Mul left right) rest'
restTerm left (Located _ TSlash : rest) = do
(right, rest') <- parseFactor rest
restTerm (Div left right) rest'
restTerm left rest = Right (left, rest)
parseFactor :: [Located] -> Either String (Expr, [Located])
parseFactor (Located _ (TNum n) : rest) = Right (Num n, rest)
parseFactor (Located _ TLParen : rest) = do
(expr, rest') <- parseExpr rest
case rest' of
Located _ TRParen : rest'' -> Right (expr, rest'')
_ -> Left "expected )"
parseFactor [] = Left "unexpected end of input"
parseFactor (Located position token : _) =
Left ("unexpected " ++ describe token ++ " at column " ++ show position)
describe :: Token -> String
describe (TNum n) = "number " ++ show n
describe TPlus = "+"
describe TMinus = "-"
describe TStar = "*"
describe TSlash = "/"
describe TLParen = "("
describe TRParen = ")"
describe (TBad c) = "character " ++ show c
parseAll :: String -> Either String Expr
parseAll input = do
(expr, rest) <- parseExpr (lexer input)
case rest of
[] -> Right expr
Located position token : _ ->
Left ("unexpected " ++ describe token ++ " at column " ++ show position)
main :: IO ()
main = do
args <- getArgs
case args of
[input] ->
case parseAll input of
Left message -> do
hPutStrLn stderr ("error: " ++ message)
exitWith (ExitFailure 2)
Right expr -> print expr
_ -> hPutStrLn stderr "usage: errors EXPR"
ghc -Wall -O1 -o errors errors.hs
./errors "1+2*3"
./errors "1+2*"
echo "status: $?"
./errors "1+@"
echo "status: $?"
Add (Num 1) (Mul (Num 2) (Num 3))
error: unexpected end of input
status: 2
error: unexpected character '@' at column 3
status: 2
The message and the status
- A good expression prints the tree and leaves status 0.
- A truncated expression names the missing part and leaves status 2.
- An unknown character is reported with its column, which is the one thing a user needs to fix it.
The next lesson puts this evaluator behind a loop, so a person can type one expression per line.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the lexer record with each token now?
Answer
The column it starts at
The position is what the message needs to point at the problem.
-
What does the program do with a bad expression?
Answer
Prints the message on stderr and exits with status 2
The error path is the usage convention of the arguments lesson.
-
What is the column of the first character of an expression?
Answer
1
Columns are one-based, as in an editor.
-
What does this program print?
module Main (main) where main :: IO () main = print (1 + 2 * 3 :: Int)Answer
7
The product is 6 and the sum is 7.
-
Why is an unknown character a token and not an error call?
Answer
The parser can report it with a position instead of crashing
A value the parser sees is a message the user can read.
-
What does this program print?
module Main (main) where main :: IO () main = print (length "1+@")Answer
3
The string has three characters.
-
What does the status of a good expression mean?
Answer
0, success
The success path leaves the default status of 0.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn "error: x"Answer
error: x
putStrLn prints the string without quotes.
-
What does
describedo in the error path?Answer
Turns the token into the word the message uses
The message says character and the value, not the constructor name.
-
What does this program print?
module Main (main) where main :: IO () main = print (10 - 4 :: Int)Answer
6
Ten minus four is six.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev