-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Package Specific Lexer/Rules and Refactor Idris/Package
- Loading branch information
Showing
16 changed files
with
234 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,76 @@ | ||
module Parser.Lexer.Package | ||
|
||
import public Parser.Lexer.Common | ||
import public Text.Lexer | ||
import public Text.Parser | ||
|
||
import Data.List | ||
import Data.Strings | ||
import Data.String.Extra | ||
import Utils.String | ||
|
||
%default total | ||
|
||
public export | ||
data PackageToken | ||
= Comment | ||
| Ident | ||
| Module | ||
| StringLit | ||
= Comment String | ||
| EndOfInput | ||
| NamespacedIdent (List String) | ||
| Module (List String) | ||
| PackageName String | ||
| Property String | ||
| Separator | ||
| Space | ||
| StringLit String | ||
|
||
public export | ||
Show PackageToken where | ||
show (Comment str) = "Comment: " ++ str | ||
show EndOfInput = "EndOfInput" | ||
show (NamespacedIdent nsid) = "Identifier " ++ dotSep nsid | ||
show (Module m) = "Module: " ++ dotSep m | ||
show (PackageName n) = "Package name: " ++ n | ||
show (Property p) = "Property: " ++ p | ||
show Separator = "Separator" | ||
show Space = "Space" | ||
show (StringLit s) = "StringLit: " ++ s | ||
|
||
packageIdent : Lexer | ||
packageIdent = exact "package" <+> is ' ' <+> identAllowDashes | ||
|
||
propertyAssignment : Lexer | ||
propertyAssignment = identAllowDashes <+> many (space <|> newline) <+> is '=' | ||
|
||
separator : Lexer | ||
separator = is ',' | ||
|
||
rawTokens : TokenMap PackageToken | ||
rawTokens = | ||
[ (comment, Comment . drop 2) | ||
, (moduleIdent, Module . splitNamespace) | ||
, (namespacedIdent, NamespacedIdent . splitNamespace) | ||
, (packageIdent, PackageName . drop 8) | ||
, (propertyAssignment, \x => case head' . words $ x of | ||
Just p => Property p | ||
-- TODO: Mark as impossible case due to ident lexer. | ||
Nothing => Property "") | ||
, (separator, const Separator) | ||
, (spaceOrNewline, const Space) | ||
, (stringLit, \s => StringLit (stripQuotes s)) | ||
] | ||
where | ||
splitNamespace : String -> List String | ||
splitNamespace = Data.Strings.split (== '.') | ||
|
||
export | ||
lex : String -> Either (Int, Int, String) (List (TokenData PackageToken)) | ||
lex str = | ||
case lexTo (const False) rawTokens str of | ||
(tokenData, (l, c, "")) => | ||
Right $ (filter (useful . tok) tokenData) ++ [MkToken l c EndOfInput] | ||
(_, fail) => Left fail | ||
where | ||
useful : PackageToken -> Bool | ||
useful (Comment c) = False | ||
useful Space = False | ||
useful _ = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Parser.Package | ||
|
||
import public Parser.Lexer.Package | ||
import public Parser.Rule.Package | ||
import public Text.Lexer | ||
import public Text.Parser | ||
import public Parser.Support | ||
|
||
import System.File | ||
import Utils.Either | ||
|
||
export | ||
runParser : String -> PackageRule ty -> Either (ParseError PackageToken) ty | ||
runParser str p | ||
= do toks <- mapError LexFail $ lex str | ||
parsed <- mapError toGenericParsingError $ parse p toks | ||
Right (fst parsed) | ||
|
||
export | ||
parseFile : (fn : String) -> PackageRule ty -> IO (Either (ParseError PackageToken) ty) | ||
parseFile fn p | ||
= do Right str <- readFile fn | ||
| Left err => pure (Left (FileFail err)) | ||
pure (runParser str p) |
Oops, something went wrong.