Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:16:52+00:00 2026-06-11T15:16:52+00:00

I’m experimenting with Trifecta for parsing a very simple functional language with Haskell-like layout

  • 0

I’m experimenting with Trifecta for parsing a very simple functional language with Haskell-like layout syntax. I’m working off the Haddock docs and my experience with Parsec, because I couldn’t find any introductory material on Trifecta itself.

The problem I’m having is with using the layout stuff, since not even the Haddock docs help much.

Given the following code:

import Text.Trifecta
import Text.Trifecta.Parser.Token.Style
import Text.Trifecta.Parser.Identifier.Style
import Text.Trifecta.Layout.Combinators
import Text.Trifecta.Language.Prim

import Control.Applicative
import Control.Monad.Trans
import Data.Maybe (fromMaybe)

import Data.HashSet as HashSet
import Data.ByteString.UTF8 as UTF8

-- Copypasta from Text.Trifecta.Parser.Identifier.Style
set :: [String] -> HashSet ByteString
set = HashSet.fromList . fmap UTF8.fromString

lang :: MonadParser m => LanguageDef m
lang = LanguageDef{ languageCommentStyle = haskellCommentStyle
                  , languageIdentifierStyle = emptyIdents{ styleReserved = set keywords }
                  , languageOperatorStyle = emptyOps{ styleReserved = set ops }
                  }
  where
    keywords = ["where"]
    ops = ["="]

data Def = Def ByteString [ByteString] [ByteString] [Def]
         deriving Show

instance MonadLanguage m => MonadLanguage (Layout m) where
    askLanguage = fmap liftLanguageDef $ lift askLanguage

def :: (MonadParser m) => Layout (Language m) Def
def = Def <$> identifier <*> vars <* reservedOp "=" <*> vars <*> laidout locals
  where
    vars = many identifier
    locals = fromMaybe [] <$> optional (reserved "where" *> defs)

defs :: (MonadParser m) => Layout (Language m) [Def]
defs = laidout (many def)

test :: String -> IO ()
test =  parseTest $ run $ defs <* eof
  where
    run p = runLanguage (fst <$> runLayout p defaultLayoutState) lang

I’m trying to parse the following text with test:

f x = x y a b c -- 1
  where         -- 2
    y = d       -- 3
g x = z         -- 4

but it fails with this parse error:

(interactive):4:2: error: expected: "=",
    identifier, letter or digit
g x = z         -- 4 
 ^      

but if I comment out lines 2 and 3, it works.

So how do I make it parse even when including lines 2 and 3?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:16:53+00:00Added an answer on June 11, 2026 at 3:16 pm

    There are a couple of bugs in the Layout support for Trifecta. I need to port some fixes back from the Scala implementation.

    Notably a few things need to disable the start of line check that do not, so it tried to do automatic-semicolon insertion where you are getting the error.

    I ported it from another codebase of mine that made slightly different assumptions and offered slightly different invariants than trifecta does.

    This is actually a large part of why it isn’t currently in the master branch on github.

    Fixing it up to use the new parsers-based API, and porting those changes is the last major thing I need to do before I can ship 0.90.

    In the meantime I wouldn’t rely on it. My apologies.

    I’ll update this response as soon as it is stable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.