Skip to content

Commit

Permalink
Improved Python type hinting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgene committed Sep 9, 2024
1 parent b6004d1 commit 03bf91d
Showing 1 changed file with 79 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,48 @@ import SyntaxHighlight.Model exposing (Token, TokenType(..))

-- Author: brandly (https://github.com/brandly)
-- TODO field declaration, reference
parseTokensReversed : String -> Result Error (List Token)
parseTokensReversed =
Parser.run
( map
( List.reverse >> List.concat )
( repeat zeroOrMore mainLoop )
)


mainLoop : Parser (List Token)
mainLoop =
topLevelLoop : Bool -> Maybe Char -> Parser (List Token)
topLevelLoop includeTypeReference groupClose =
oneOf
[ whitespaceOrComment
, stringLiteral
, oneOf [ symbol ":", symbol "->" ]
, oneOf (if includeTypeReference then [ symbol ":", symbol "->" ] else [])
|> source
|> andThen typeReferenceLoop
, symbol "@"
|> source
|> andThen decoratorLoop
, symbol "{"
|> source
|> andThen
( \c ->
topLevelLoop False (Just '}')
|> repeat zeroOrMore
|> consThenRevConcat [ ( Operator, c ) ]
)
, symbol "["
|> source
|> andThen
( \c ->
topLevelLoop False (Just ']')
|> repeat zeroOrMore
|> consThenRevConcat [ ( Operator, c ) ]
)
, symbol "("
|> source
|> andThen
( \c ->
topLevelLoop False (Just ')')
|> repeat zeroOrMore
|> consThenRevConcat [ ( Operator, c ) ]
)
, oneOf
[ operatorChar
, groupChar
( case groupClose of
Just c -> Set.singleton c
Nothing -> Set.empty
)
, number
]
|> map List.singleton
Expand All @@ -45,6 +64,19 @@ mainLoop =
]


mainLoop : Parser (List Token)
mainLoop = topLevelLoop True Nothing


parseTokensReversed : String -> Result Error (List Token)
parseTokensReversed =
Parser.run
( map
( List.reverse >> List.concat )
( repeat zeroOrMore mainLoop )
)


keywordParser : String -> Parser (List Token)
keywordParser n =
if n == "def" then
Expand All @@ -55,6 +87,10 @@ keywordParser n =
classDeclarationLoop
|> repeat zeroOrMore
|> consThenRevConcat [ ( DeclarationKeyword, n ) ]
else if n == "lambda" then
lambdaDeclarationArgLoop
|> repeat zeroOrMore
|> consThenRevConcat [ ( DeclarationKeyword, n ) ]
else if isKeyword n then
succeed [ ( Keyword, n ) ]
else if isBuiltIn n then
Expand All @@ -78,15 +114,15 @@ functionDeclarationLoop =
, symbol "("
|> andThen
( \_ ->
argLoop
functionDeclarationArgLoop
|> repeat zeroOrMore
|> consThenRevConcat [ ( Operator, "(" ) ]
)
]


argLoop : Parser (List Token)
argLoop =
functionDeclarationArgLoop : Parser (List Token)
functionDeclarationArgLoop =
oneOf
[ whitespaceOrComment
, keep oneOrMore (\c -> not (isCommentChar c || isWhitespace c || c == ':' || c == ',' || c == ')'))
Expand All @@ -99,6 +135,17 @@ argLoop =
]


lambdaDeclarationArgLoop : Parser (List Token)
lambdaDeclarationArgLoop =
oneOf
[ whitespaceOrComment
, keep oneOrMore isIdentifierNameChar
|> map (\name -> [ ( FunctionArgument, name ) ])
, keep oneOrMore (\c -> c == ',' || c == ':')
|> map (\sep -> [ ( Operator, sep ) ])
]


functionEvalLoop : String -> List Token -> Parser (List Token)
functionEvalLoop identifier revTokens =
oneOf
Expand Down Expand Up @@ -143,7 +190,7 @@ typeReferenceInnerLoop groupCloses =
if isBuiltIn name then [ ( BuiltIn, name ) ]
else [ ( TypeReference, name ) ]
)
, oneOf [ symbol "|" ]
, oneOf [ symbol "|", symbol "." ]
|> source
|> map ( \op -> [ ( Operator, op ) ] )
, symbol "["
Expand Down Expand Up @@ -234,7 +281,7 @@ isBuiltIn str = Set.member str builtInSet

builtInSet : Set String
builtInSet =
Set.fromList [ "bool", "dict", "float", "int", "list", "object", "str" ]
Set.fromList [ "bool", "dict", "float", "int", "list", "object", "set", "str" ]


isPunctuation : Char -> Bool
Expand Down Expand Up @@ -280,15 +327,15 @@ operatorSet =
]


groupChar : Parser Token
groupChar =
keep oneOrMore isGroupChar
groupChar : Set Char -> Parser Token
groupChar except =
keep oneOrMore (isGroupChar except)
|> map (\c -> ( Operator, c ))


isGroupChar : Char -> Bool
isGroupChar c =
Set.member c groupSet
isGroupChar : Set Char -> Char -> Bool
isGroupChar except c =
Set.member c (Set.diff groupSet except)


groupSet : Set Char
Expand Down Expand Up @@ -388,6 +435,14 @@ multilineComment =
}


backspace : Parser (List Token)
backspace =
symbol "\\"
|. ignore zeroOrMore (not << isLineBreak)
|> source
|> map ( \c -> [ ( Comment, c ) ] )


isCommentChar : Char -> Bool
isCommentChar c = c == '#'

Expand All @@ -401,6 +456,7 @@ whitespaceOrComment =
|> map ( \c -> [ ( Normal, c ) ] )
, lineBreak
, comment
, backspace
]


Expand Down

0 comments on commit 03bf91d

Please sign in to comment.