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

Understanding on what lit and src behaviors #41

Open
Rizary opened this issue Jul 3, 2017 · 4 comments
Open

Understanding on what lit and src behaviors #41

Rizary opened this issue Jul 3, 2017 · 4 comments

Comments

@Rizary
Copy link

Rizary commented Jul 3, 2017

@natefaubion, continuing my chat in slack, and trying to avoid slack archive.. i'll put my reply in here.

Here is the guide said:

```  -- | `lit x` will match exactly the path component `x`.
  -- | For example, `lit "x"` matches `/x`.
  lit :: String -> f Unit

  -- | `str` matches any path string component.
  -- | For example, `str` matches `/foo` as `"foo"`.
  str :: f String```

I just want to correct my logic in how purescript-routing act. So, my understanding is, the library trying to match the uri address with the pattern we describe in routing :: Match a function. How it works is it stripped the uri address (let say mysite.com/#/foo/bar/FooBar) until the # sign.

So, we will have #/foo/bar/FooBar, which in this case, if we use the above guide, lit "x" matches "/x" and "str" matches "/foo" as "foo", then the stripped address can be matched with this:

#/foo        /bar         /FooBar
  (lit "foo") (lit "bar") (lit "FooBar")

but instead, i have to do this:

#/foo                     /bar                    /FooBar
  (lit "" <* lit "foo") (lit "" <*lit "bar") (lit "" <* lit "FooBar")

and if we see the guide again, it looks like it is "//foo//bar//FooBar". I know that we use <* or *> that supposed to remove the lit "". But why can't we just use the former example one??? I mean why we can't just write it :
FooBarPage <$ (lit "foo" *> lit "bar" *> lit "FooBar" )

Instead of:

FooBarPage <$ (lit "" *> lit "foo" *> lit "" *> lit "bar" *> lit "" *> lit "FooBar" )

the same goes if we have FooBarPage String, by using the guide again, we supposed to write:

FooBarPage <$> (lit "foo" *> lit "bar" *> lit "" *> lit "FooBar" )

instead of
FooBarPage <$ (lit "" *> lit "foo" *> lit "" *> lit "bar" *> lit "FooBar" )

Right? I am sorry if my understanding is not what the guide supposed to mean. But from beginner pov (like me), reading the source's comment explanation is very helpful (well i have to try and error by building it though for better understanding).

Anyway, this is just me trying to visualise what the lib do.

@natefaubion
Copy link
Collaborator

natefaubion commented Jul 3, 2017

The parser matches on slugs in between slashes (or rather, it's tokenized using slashes as delimiters). lit "bar" matches bar literally, not /bar. So given foo/bar/baz, the parser matches on tokens [ "foo", "bar", "baz" ]. Given /foo/bar/baz, the parser matches on tokens [ "", "foo", "bar", "baz" ], which is why you need lit "" to match the root slash.

@Rizary
Copy link
Author

Rizary commented Jul 3, 2017

i see.. so we need the lit "" only at the first time, not every / that we have..

And also it means, we cannot parse if there isn't any / in the address. For an example i want to parse like the following url:

https://matrix.hackage.haskell.org/#/package/a50#GHC-7.6/a50-0.2

as there is not / in between the a50 and GHC-70, the a50#GHC-7.6 will be tokenized as one string? How can we dealing with this case?

@natefaubion
Copy link
Collaborator

I think that https://matrix.hackage.haskell.org/#/package/a50#GHC-7.6/a50-0.2 would likely be tokenized as [ "", "package", "a50#GHC-7.6", "a50-0.2" ]. The parser does not recursively tokenize on #, it just drops everything until the first #.

@Rizary
Copy link
Author

Rizary commented Jul 4, 2017

@natefaubion noted and thank you..

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

No branches or pull requests

2 participants