Skip to content

Commit

Permalink
Implement Parser.Advanced.token and demo composite integer parser
Browse files Browse the repository at this point in the history
+ Automate tests using a composite integer parser supporting an optional negation token.
  • Loading branch information
Viir committed Sep 27, 2024
1 parent fda2732 commit 8cbe666
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,51 @@ chompBase10Helper offset chars =
offset
isSubString : String -> Int -> Int -> Int -> String -> (Int, Int, Int)
isSubString : String -> Int -> Int -> Int -> String -> ( Int, Int, Int )
isSubString (String smallChars) offset row col (String bigChars) =
-- TODO
(-11, -13, -17)
let
bigCharsFromOffset =
List.drop offset bigChars
( newOffset, newRow, newCol, matched ) =
isSubStringHelper smallChars bigCharsFromOffset offset row col
in
if matched then
( newOffset, newRow, newCol )
else
( -1, row, col )
isSubStringHelper : List Char -> List Char -> Int -> Int -> Int -> ( Int, Int, Int, Bool )
isSubStringHelper smallChars bigChars offset row col =
case ( smallChars, bigChars ) of
( [], _ ) ->
( offset, row, col, True )
( _, [] ) ->
( offset, row, col, False )
( sChar :: sRest, bChar :: bRest ) ->
if Pine_kernel.equal [ sChar, bChar ] then
let
( newRow, newCol ) =
if Pine_kernel.equal [ sChar, 10 ] then
-- ASCII code for '\\n'
( Pine_kernel.add_int [ row, 1 ], 1 )
else
( row, Pine_kernel.add_int [ col, 1 ] )
in
isSubStringHelper
sRest
bRest
(Pine_kernel.add_int [offset, 1])
newRow
newCol
else
( offset, row, col, False )
isSubChar : (Char -> Bool) -> Int -> String -> Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ signedInt =
|= Parser.Advanced.int () ()
, Parser.Advanced.int () ()
]


negatedInt : Parser.Advanced.Parser () () Int
negatedInt =
Parser.Advanced.succeed negate
|. Parser.Advanced.symbol negateToken
|= Parser.Advanced.int () ()


signedInt_without_infix : Parser.Advanced.Parser () () Int
signedInt_without_infix =
Parser.Advanced.oneOf
[ (|=)
((|.)
(Parser.Advanced.succeed negate)
(Parser.Advanced.symbol negateToken)
)
(Parser.Advanced.int () ())
, Parser.Advanced.int () ()
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ok []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parser.Advanced.run (Parser.Advanced.symbol CompositeParser.negateToken) "-"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ok 0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parser.Advanced.run CompositeParser.signedInt "0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ok -1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parser.Advanced.run CompositeParser.signedInt "-1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ok -615
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parser.Advanced.run CompositeParser.signedInt "-615"

0 comments on commit 8cbe666

Please sign in to comment.