Skip to content
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

Questions to which this is the answer #3

Open
jamesdbrock opened this issue Aug 18, 2019 · 31 comments
Open

Questions to which this is the answer #3

jamesdbrock opened this issue Aug 18, 2019 · 31 comments

Comments

@jamesdbrock
Copy link
Member Author

Megaparsec parsers are aware
of line numbers, and the Parsereplace.streamEdit function allows you to
edit pattern matches in a stream. Here is a solution
using Parsereplace.streamEdit.

import Parsereplace
import Text.Megaparsec
import Text.Megaparsec.Char

let a = "\n\n\nShape(Rectangle 3 5)"

:{
let pattern :: Parsec Void String (Int,String)
pattern = do
string "Shape("
lineNumber <- unPos <$> sourceLine <$> getSourcePos
parenInner <- many $ noneOf ")"
string ")"
return (lineNumber, parenInner)
:}

:{
let editor (lineNumber, parenInner) =
"TrackedShape(" ++
show lineNumber ++
" "John" (" ++
parenInner ++
"))"
:}

streamEdit pattern editor a

"\n\n\nTrackedShape(4 \"John\" (Rectangle 3 5))"

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 21, 2019

@jamesdbrock
Copy link
Member Author

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 24, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 27, 2019

https://stackoverflow.com/questions/29546940/parsec-ignore-everything-except-one-fragment

You can use Replace.Megaparsec.findall to find the substrings in a document which match a parser.

import Replace.Megaparsec
import Text.Megaparsec

let parseSelect :: Parsec Void String String
    parseSelect = do
        chunk "<select"
        manyTill anySingle $ chunk "</select>"
let input = "<html>\n   random content with lots of tags...\n   <select id=something title=\"whatever\"><option value=1 selected>1. First<option value=2>2. Second</select>\n   more random content...\n</html>"
>>> parseTest (findAll parseSelect) input
[Left "<html>\n   random content with lots of tags...\n   "
,Right "<select id=something title=\"whatever\"><option value=1 selected>1. First<option value=2>2. Second</select>"
,Left "\n   more random content...\n</html>"
]

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 28, 2019

https://stackoverflow.com/questions/57667534/how-can-i-use-a-parser-in-haskell-to-find-the-locations-of-some-substrings-in-a

-- We can do this with the [`findAllCap`](https://hackage.haskell.org/package/replace-megaparsec/docs/Replace-Megaparsec.html#v:findAllCap) combinator from [replace-megaparsec](https://hackage.haskell.org/package/replace-megaparsec/).

-- Requires packages `megaparsec, replace-megaparsec, containers`

import Replace.Megaparsec
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Maybe
import Data.Either
import Data.Map.Strict as Map

:{
let colorWords :: Parsec Void String (String, [Int])
    colorWords = do
            i <- getOffset
            c <- choice
                [ try $ string' "blue" >>
                        anySingle >>
                        string' "green" >>
                        pure "blue green"
                , try $ string' "blue" >> pure "blue"
                , try $ string' "green" >> pure "green"
                ]
            return (c,[i])
:}

input = "First there was blue\nand then there was Green,\nand then blue\ngreen all of a sudden, and not to mention blue-green"

Map.toList $ fromListWith mappend $ rights $ fromJust $ parseMaybe (sepCap colorWords) input

-- [("blue",[16]),("blue green",[103,56]),("green",[40])]

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 28, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 28, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 28, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 29, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 30, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 30, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Aug 31, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Sep 5, 2019

https://stackoverflow.com/questions/18951235/searching-for-a-pattern-with-parsec
(search stackoverflow for "regex-applicative")

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Sep 6, 2019

@jamesdbrock
Copy link
Member Author

jamesdbrock commented Sep 7, 2019

@jamesdbrock
Copy link
Member Author

@jamesdbrock
Copy link
Member Author

@jamesdbrock
Copy link
Member Author

@jamesdbrock
Copy link
Member Author

@jamesdbrock
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant