-
Notifications
You must be signed in to change notification settings - Fork 44
Improve the performance of the Kore parser #2296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
69c9474
inlined `space` and `stringParserToIdParser` Lexer functions
MirceaS 69ad76e
Merge branch 'master' into 2189
ttuegel 663cfc5
Kore.Parser.Lexer: Remove unused primitive parsers
ttuegel 3047aa1
Run Parser over Text, not String
ttuegel 900d983
fixed unit tests
MirceaS bc05ca9
addressed comments
MirceaS 54d255f
Merge branch 'master' into 2189
MirceaS 1ba70f8
addressed PR comments
MirceaS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,8 +25,6 @@ module Kore.Parser.Lexer | |||||||||||||||
, parseId | ||||||||||||||||
, parseAnyId, parseSetId, isSymbolId | ||||||||||||||||
, isElementVariableId, isSetVariableId | ||||||||||||||||
, elementVariableIdParser | ||||||||||||||||
, setVariableIdParser | ||||||||||||||||
, parseSortId | ||||||||||||||||
, parseSymbolId | ||||||||||||||||
, parseModuleName | ||||||||||||||||
|
@@ -48,6 +46,9 @@ import Data.Map.Strict | |||||||||||||||
( Map | ||||||||||||||||
) | ||||||||||||||||
import qualified Data.Map.Strict as Map | ||||||||||||||||
import Data.Text | ||||||||||||||||
( Text | ||||||||||||||||
) | ||||||||||||||||
import qualified Data.Text as Text | ||||||||||||||||
import Text.Megaparsec | ||||||||||||||||
( SourcePos (..) | ||||||||||||||||
|
@@ -84,6 +85,7 @@ space = L.space Parser.space1 lineComment blockComment | |||||||||||||||
where | ||||||||||||||||
lineComment = L.skipLineComment "//" | ||||||||||||||||
blockComment = L.skipBlockComment "/*" "*/" | ||||||||||||||||
{-# INLINE space #-} | ||||||||||||||||
|
||||||||||||||||
{- | Parse the character, but skip its result. | ||||||||||||||||
-} | ||||||||||||||||
|
@@ -97,7 +99,7 @@ skipChar = Monad.void . Parser.char | |||||||||||||||
See also: 'L.symbol', 'space' | ||||||||||||||||
|
||||||||||||||||
-} | ||||||||||||||||
symbol :: String -> Parser () | ||||||||||||||||
symbol :: Text -> Parser () | ||||||||||||||||
symbol = Monad.void . L.symbol space | ||||||||||||||||
|
||||||||||||||||
colon :: Parser () | ||||||||||||||||
|
@@ -163,7 +165,7 @@ consumes any trailing whitespace. | |||||||||||||||
See also: 'space' | ||||||||||||||||
|
||||||||||||||||
-} | ||||||||||||||||
keyword :: String -> Parser () | ||||||||||||||||
keyword :: Text -> Parser () | ||||||||||||||||
keyword s = lexeme $ do | ||||||||||||||||
_ <- Parser.chunk s | ||||||||||||||||
-- Check that the next character cannot be part of an @id@, i.e. check that | ||||||||||||||||
|
@@ -183,19 +185,16 @@ sourcePosToFileLocation | |||||||||||||||
, column = unPos column' | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
{- Takes a parser for the string of the identifier | ||||||||||||||||
and returns an 'Id' annotated with position. | ||||||||||||||||
-} | ||||||||||||||||
stringParserToIdParser :: Parser String -> Parser Id | ||||||||||||||||
stringParserToIdParser stringRawParser = do | ||||||||||||||||
{- | Annotate a 'Text' parser with an 'AstLocation'. | ||||||||||||||||
-} | ||||||||||||||||
parseIntoId :: Parser Text -> Parser Id | ||||||||||||||||
parseIntoId stringRawParser = do | ||||||||||||||||
!pos <- sourcePosToFileLocation <$> getSourcePos | ||||||||||||||||
name <- lexeme stringRawParser | ||||||||||||||||
return Id | ||||||||||||||||
{ getId = Text.pack name | ||||||||||||||||
, idLocation = AstLocationFile pos | ||||||||||||||||
} | ||||||||||||||||
getId <- lexeme stringRawParser | ||||||||||||||||
return Id { getId, idLocation = AstLocationFile pos } | ||||||||||||||||
{-# INLINE parseIntoId #-} | ||||||||||||||||
|
||||||||||||||||
koreKeywordsSet :: HashSet String | ||||||||||||||||
koreKeywordsSet :: HashSet Text | ||||||||||||||||
koreKeywordsSet = HashSet.fromList keywords | ||||||||||||||||
where | ||||||||||||||||
keywords = | ||||||||||||||||
|
@@ -224,17 +223,17 @@ genericIdRawParser | |||||||||||||||
:: (Char -> Bool) -- ^ contains the characters allowed for @⟨prefix-char⟩@. | ||||||||||||||||
-> (Char -> Bool) -- ^ contains the characters allowed for @⟨body-char⟩@. | ||||||||||||||||
-> IdKeywordParsing | ||||||||||||||||
-> Parser String | ||||||||||||||||
-> Parser Text | ||||||||||||||||
genericIdRawParser isFirstChar isBodyChar idKeywordParsing = do | ||||||||||||||||
c <- Parser.satisfy isFirstChar <?> "first identifier character" | ||||||||||||||||
cs <- Parser.takeWhileP (Just "identifier character") isBodyChar | ||||||||||||||||
let genericId = c : cs | ||||||||||||||||
keywordsForbidden = idKeywordParsing == KeywordsForbidden | ||||||||||||||||
(genericId, _) <- Parser.match | ||||||||||||||||
$ (Parser.satisfy isFirstChar <?> "first identifier character") | ||||||||||||||||
>> Parser.takeWhileP (Just "identifier character") isBodyChar | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is easier to read:
Suggested change
|
||||||||||||||||
let keywordsForbidden = idKeywordParsing == KeywordsForbidden | ||||||||||||||||
isKeyword = HashSet.member genericId koreKeywordsSet | ||||||||||||||||
when (keywordsForbidden && isKeyword) | ||||||||||||||||
$ fail | ||||||||||||||||
( "Identifiers should not be keywords: '" | ||||||||||||||||
++ genericId | ||||||||||||||||
++ Text.unpack genericId | ||||||||||||||||
++ "'." | ||||||||||||||||
) | ||||||||||||||||
return genericId | ||||||||||||||||
|
@@ -293,11 +292,14 @@ isIdChar c = isIdFirstChar c || isIdOtherChar c | |||||||||||||||
An identifier cannot be a keyword. | ||||||||||||||||
-} | ||||||||||||||||
parseId :: Parser Id | ||||||||||||||||
parseId = stringParserToIdParser (parseIdRaw KeywordsForbidden) | ||||||||||||||||
parseId = parseIntoId parseIdText | ||||||||||||||||
|
||||||||||||||||
parseIdRaw :: IdKeywordParsing -> Parser String | ||||||||||||||||
parseIdRaw :: IdKeywordParsing -> Parser Text | ||||||||||||||||
parseIdRaw = genericIdRawParser isIdFirstChar isIdChar | ||||||||||||||||
|
||||||||||||||||
parseIdText :: Parser Text | ||||||||||||||||
parseIdText = parseIdRaw KeywordsForbidden | ||||||||||||||||
|
||||||||||||||||
{- | Parse a module name. | ||||||||||||||||
|
||||||||||||||||
@ | ||||||||||||||||
|
@@ -309,7 +311,7 @@ parseModuleName = lexeme moduleNameRawParser | |||||||||||||||
|
||||||||||||||||
moduleNameRawParser :: Parser ModuleName | ||||||||||||||||
moduleNameRawParser = | ||||||||||||||||
ModuleName . Text.pack <$> parseIdRaw KeywordsForbidden | ||||||||||||||||
ModuleName <$> parseIdRaw KeywordsForbidden | ||||||||||||||||
|
||||||||||||||||
{- | Parses a 'Sort' 'Id' | ||||||||||||||||
|
||||||||||||||||
|
@@ -321,7 +323,9 @@ parseSortId :: Parser Id | |||||||||||||||
parseSortId = parseId <?> "sort identifier" | ||||||||||||||||
|
||||||||||||||||
parseAnyId :: Parser Id | ||||||||||||||||
parseAnyId = (parseSpecialId <|> parseSetId <|> parseId) <?> "identifier" | ||||||||||||||||
parseAnyId = parseIntoId | ||||||||||||||||
(parseSpecialIdText <|> parseSetIdText <|> parseIdText) | ||||||||||||||||
<?> "identifier" | ||||||||||||||||
|
||||||||||||||||
isSymbolId :: Id -> Bool | ||||||||||||||||
isSymbolId Id { getId } = | ||||||||||||||||
|
@@ -336,19 +340,16 @@ isElementVariableId Id { getId } = | |||||||||||||||
isSetVariableId :: Id -> Bool | ||||||||||||||||
isSetVariableId Id { getId } = Text.head getId == '@' | ||||||||||||||||
|
||||||||||||||||
parseSpecialId :: Parser Id | ||||||||||||||||
parseSpecialId = | ||||||||||||||||
stringParserToIdParser parseSpecialIdString | ||||||||||||||||
where | ||||||||||||||||
parseSpecialIdString = | ||||||||||||||||
(:) <$> Parser.char '\\' <*> parseIdRaw KeywordsPermitted | ||||||||||||||||
parseSpecialIdText :: Parser Text | ||||||||||||||||
parseSpecialIdText = fst <$> Parser.match | ||||||||||||||||
(Parser.char '\\' >> parseIdRaw KeywordsPermitted) | ||||||||||||||||
|
||||||||||||||||
parseSetIdText :: Parser Text | ||||||||||||||||
parseSetIdText = fst <$> Parser.match | ||||||||||||||||
(Parser.char '@' >> parseIdRaw KeywordsPermitted) | ||||||||||||||||
|
||||||||||||||||
parseSetId :: Parser Id | ||||||||||||||||
parseSetId = | ||||||||||||||||
stringParserToIdParser parseSetIdString | ||||||||||||||||
where | ||||||||||||||||
parseSetIdString = | ||||||||||||||||
(:) <$> Parser.char '@' <*> parseIdRaw KeywordsPermitted | ||||||||||||||||
parseSetId = parseIntoId parseSetIdText | ||||||||||||||||
|
||||||||||||||||
{- | Parses a 'Symbol' 'Id' | ||||||||||||||||
|
||||||||||||||||
|
@@ -357,42 +358,16 @@ parseSetId = | |||||||||||||||
@ | ||||||||||||||||
-} | ||||||||||||||||
parseSymbolId :: Parser Id | ||||||||||||||||
parseSymbolId = | ||||||||||||||||
stringParserToIdParser symbolIdRawParser <?> "symbol or alias identifier" | ||||||||||||||||
parseSymbolId = parseIntoId symbolIdRawParser <?> "symbol or alias identifier" | ||||||||||||||||
|
||||||||||||||||
symbolIdRawParser :: Parser String | ||||||||||||||||
symbolIdRawParser :: Parser Text | ||||||||||||||||
symbolIdRawParser = do | ||||||||||||||||
c <- peekChar' | ||||||||||||||||
if c == '\\' | ||||||||||||||||
then do | ||||||||||||||||
skipChar '\\' | ||||||||||||||||
(c :) <$> parseIdRaw KeywordsPermitted | ||||||||||||||||
then fst <$> Parser.match | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||||||||||||||||
(Parser.char '\\' >> parseIdRaw KeywordsPermitted) | ||||||||||||||||
else parseIdRaw KeywordsForbidden | ||||||||||||||||
|
||||||||||||||||
{-|Parses a @set-variable-id@, which always starts with @\@@. | ||||||||||||||||
|
||||||||||||||||
@ | ||||||||||||||||
<set-variable-id> ::= ['@'] <id> | ||||||||||||||||
@ | ||||||||||||||||
-} | ||||||||||||||||
setVariableIdParser :: Parser Id | ||||||||||||||||
setVariableIdParser = stringParserToIdParser setVariableIdRawParser | ||||||||||||||||
|
||||||||||||||||
setVariableIdRawParser :: Parser String | ||||||||||||||||
setVariableIdRawParser = do | ||||||||||||||||
start <- Parser.char '@' | ||||||||||||||||
end <- parseIdRaw KeywordsPermitted | ||||||||||||||||
return (start:end) | ||||||||||||||||
|
||||||||||||||||
{-| Parses an @element-variable-id@ | ||||||||||||||||
|
||||||||||||||||
@ | ||||||||||||||||
<element-variable-id> ::= <id> | ||||||||||||||||
@ | ||||||||||||||||
-} | ||||||||||||||||
elementVariableIdParser :: Parser Id | ||||||||||||||||
elementVariableIdParser = parseId | ||||||||||||||||
|
||||||||||||||||
{- | Parses a C-style string literal, unescaping it. | ||||||||||||||||
|
||||||||||||||||
@ | ||||||||||||||||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ttuegel Why are these the other way round now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it from
($)
to(&)
so that the action (Text.readFile
) would appear before the wrappers.