Comments (5)
I am currently contemplating how to implement this. The problem is that with the overloaded labels it is inherently ambiguous for any label that occurs as the leftmost lexeme whether it is a label or a c preprocessor instruction.
I see three possible ways of resolving this:
- Bias: Either leave it as it is (only parse preprocessors) or parse them all as labels.
- Leftmost is C: Make all labels, unless its only preceeded by whitespace on the left.
Kinda handles both but would not do case-1 in your example correctly - Top-level is C: Only parse it as C, then it starts the line, and is not preceded by whitespace.
Handles your case-1 correctly, but will handle many actual CPP applications wrong. Might be tolerable though, since the most common use is simple, tope level#if
or#ifdef
. However may be very confusing in the cases where this does occur. - Known directive: Only parse known CPP directives. I.e.
#if
,#else
,#define
,#include
(plus only if they are leftmost, i.e. 2)
My favorite is option 4. I think this will cause the least disruption and handle almost all usual cases well.
I'd appreciate some input from you @sheaf and also @poscat0x04, since you also expressed interest in this.
from language-haskell.
I just did a silly test:
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLabels #-}
module Main where
import GHC.OverloadedLabels
instance r ~ ( Int -> IO Int -> () -> IO Int -> () -> IO Int ) => IsLabel "if" r where
fromLabel c =
if c /= 0
then \ t _ _ _ -> fmap negate t
else \ _ _ f _ -> fmap negate f
instance r ~ () => IsLabel "else" r where
fromLabel = ()
instance r ~ () => IsLabel "endif" r where
fromLabel = ()
x :: IO Int
x = do {
#if 0
( pure 1 )
#else
( pure 2 )
#endif
}
main :: IO ()
main = print =<< x
The result here is -2, and if you remove the CPP extension, it still compiles but now the result becomes 2. So in this edge case, the syntax is inherently ambiguous and depends on the enabled language extensions.
At any rate, solution (4.) seems plenty good enough, as the above is realistically not going to occur in practice... never have I come across an overloaded label starting on the first character of a line.
from language-haskell.
I added support for overloaded labels in 34f27a4. I made the decision to only allow CPP instructions starting on the beginning of a line. That is, I am using approach (2)
above. I figured it can be useful to allow other CPP directives, especially for tools like C2hs
and Hsc2Hs
(although those could have custom overriding rules).
So, other than # on the start of a line, prefix ocurrences of #
are parsed as an overloaded label.
from language-haskell.
I think that's good enough for now, but I'd like to leave this issue open as a reminder and eventually implement (4)
from language-haskell.
Actually I've decided to instead open a new issue for this, as it does not concern labels anymore but the highlighting of CPP
from language-haskell.
Related Issues (20)
- Functions & Variables Highlighting HOT 1
- Flags inside pragma HOT 2
- Type signature in pattern synonym incorrectly highlighted
- {-# SCC "Specialise" #-} messes up the color of text after it HOT 4
- Don't default to .hsig when saving files
- Hovering doesn't work anymore HOT 1
- Cabal `elif` keyword is not highlighted
- QuasiQuotes with immediate `|` are not parsed correctly HOT 2
- How to navigate to function definitions? HOT 2
- GHC2021
- Repeated Haskell-style comments in Happy quotations are treated as comments HOT 3
- Highlighting broken with version 3.5 HOT 38
- Highlighting Isnt Working on Latest Update HOT 1
- Publishing on Open VSX Registry HOT 1
- No highlighting HOT 9
- VS Web Extension Suppport HOT 4
- Mis-scope of let-binding declarations immediately followed by signatures HOT 2
- Binding patterns named `pattern` are recognized as a keyword HOT 1
- LambdaCase \cases syntax
- Incorrect highlighting for some package imports
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language-haskell.