79 lines · 2.4 KB · Text
RawDownload
||| Slightly different lexer than the source language because we are more free||| as to what can be identifiers, and fewer tokens are supported. But otherwise,||| we can reuse the standard stuffmodule Idris.IDEMode.Parser import Idris.IDEMode.Commands import Text.Parserimport Parser.Lexerimport Parser.Supportimport Text.Lexer %hide Lexer.symbols symbols : List Stringsymbols = ["(", ":", ")"] ideTokens : TokenMap TokenideTokens =    map (\x => (exact x, Symbol)) symbols ++    [(digits, \x => Literal (cast x)),     (stringLit, \x => StrLit (stripQuotes x)),     (identRelaxed, Ident),     (space, Comment)]  where    stripQuotes : String -> String    -- ASSUMPTION! Only total because we know we're getting quoted strings.    stripQuotes = assert_total (strTail . reverse . strTail . reverse) idelex : String -> Either (Int, Int, String) (List (TokenData Token))idelex str    = case lex ideTokens str of           -- Add the EndInput token so that we'll have a line and column           -- number to read when storing spans in the file           (tok, (l, c, "")) => Right (filter notComment tok ++                                      [MkToken l c EndInput])           (_, fail) => Left fail    where      notComment : TokenData Token -> Bool      notComment t = case tok t of                          Comment _ => False                          _ => True sexp : Rule SExpsexp    = do symbol ":"; exactIdent "True"         pure (BoolAtom True)  <|> do symbol ":"; exactIdent "False"         pure (BoolAtom False)  <|> do i <- intLit         pure (IntegerAtom i)  <|> do str <- strLit         pure (StringAtom str)  <|> do symbol ":"; x <- unqualifiedName         pure (SymbolAtom x)  <|> do symbol "("         xs <- many sexp         symbol ")"         pure (SExpList xs) ideParser : String -> Grammar (TokenData Token) e ty -> Either ParseError tyideParser str p    = case idelex str of           Left err => Left $ LexFail err           Right toks =>              case parse p toks of                   Left (Error err []) =>                          Left $ ParseFail err Nothing []                   Left (Error err (t :: ts)) =>                          Left $ ParseFail err (Just (line t, col t))                                               (map tok (t :: ts))                   Right (val, _) => Right val exportparseSExp : String -> Either ParseError SExpparseSExp inp    = ideParser inp (do c <- sexp; eoi; pure c)  

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t