Feature request: provide Template Haskell quasiquoter that generates lexer definition in-place
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 28/100
Hướng nghiên cứu
Bắt đầu với alex test-suite case tokens_scan_user.x và đường dẫn tiền xử lý dòng lệnh hiện có, sau đó so sánh nó với ví dụ TokensScanUser.hs được đề xuất và import Alex.TH. Hoàn tất khi một quasiquoter chấp nhận định dạng định nghĩa lexer hiện có và tạo ra các biểu thức Template Haskell tương đương mà không cần file được sinh ra; payload không nêu tên một file triển khai cụ thể hay lệnh kiểm thử nào.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Currently alex is a command-line tool that takes files in its own format and relies on support from Cabal-the-library to invoke alex command in order to preprocess .x files before passing them to ghc. This works reasonably well, but I think there's something to be gained if alex package would provide quasiquoter that takes lexer definition in the currently used format but spits out TH expressions instead of generating new file.
E.g. take tokens_scan_user.x test from the alex test suite. Instead of
{
module Main (main) where
import System.Exit
}
%wrapper "basic" -- Defines: AlexInput, alexGetByte, alexPrevChar
$digit = 0-9
$alpha = [a-zA-Z]
$ws = [\ \t\n]
tokens :-
5 / {\ u _ibt _l _iat -> u == FiveIsMagic} { \s -> TFive (head s) }
$digit { \s -> TDigit (head s) }
$alpha { \s -> TAlpha (head s) }
$ws { \s -> TWSpace (head s) }
{
data Token = TDigit Char
| TAlpha Char
| TWSpace Char
| TFive Char -- Predicated only
| TLexError
deriving (Eq,Show)
data UserLexerMode = NormalMode
| FiveIsMagic
deriving Eq
main | test1 /= result1 = exitFailure
| test2 /= result2 = exitFailure
-- all succeeded
| otherwise = exitWith ExitSuccess
run_lexer :: UserLexerMode -> String -> [Token]
run_lexer m s = go ('\n', [], s)
where go i@(_,_,s') = case alexScanUser m i 0 of
AlexEOF -> []
AlexError _i -> [TLexError]
AlexSkip i' _len -> go i'
AlexToken i' len t -> t (take len s') : go i'
test1 = run_lexer FiveIsMagic "5 x"
result1 = [TFive '5',TWSpace ' ',TAlpha 'x']
test2 = run_lexer NormalMode "5 x"
result2 = [TDigit '5',TWSpace ' ',TAlpha 'x']
}
I'd like to write TokensScanUser.hs file that looks like:
{-# LANGUAGE QuasiQuotes #-}
module Main (main) where
import System.Exit
import Alex.TH
genLexer defaultLexer [alex|
%wrapper "basic" -- Defines: AlexInput, alexGetByte, alexPrevChar
$digit = 0-9
$alpha = [a-zA-Z]
$ws = [\ \t\n]
tokens :-
5 / {\u _ibt _l _iat -> u == FiveIsMagic} { \s -> TFive (head s) }
$digit { \s -> TDigit (head s) }
$alpha { \s -> TAlpha (head s) }
$ws { \s -> TWSpace (head s) }
|]
data Token = TDigit Char
| TAlpha Char
| TWSpace Char
| TFive Char -- Predicated only
| TLexError
deriving (Eq,Show)
data UserLexerMode = NormalMode
| FiveIsMagic
deriving Eq
main | test1 /= result1 = exitFailure
| test2 /= result2 = exitFailure
-- all succeeded
| otherwise = exitWith ExitSuccess
run_lexer :: UserLexerMode -> String -> [Token]
run_lexer m s = go ('\n', [], s)
where go i@(_,_,s') = case alexScanUser m i 0 of
AlexEOF -> []
AlexError _i -> [TLexError]
AlexSkip i' _len -> go i'
AlexToken i' len t -> t (take len s') : go i'
test1 = run_lexer FiveIsMagic "5 x"
result1 = [TFive '5',TWSpace ' ',TAlpha 'x']
test2 = run_lexer NormalMode "5 x"
result2 = [TDigit '5',TWSpace ' ',TAlpha 'x']
Having such quasiquoter will provide following benefits:
- This will provide an option to use alex independent of system's preprocessor if, say,
clangstarts to behave funny - This can help end confusion of text editors with overly long lines as mentioned #84
- There will be no nowelines in the generated files (becase there will be no generated files) that may annoy someone #105
- User will be able to just start
ghciin his or hers project and load lexer definition to play with - no need to adddist/build/directory (or different directory, depending on build target and the build tool (cabalhas one prefix here,stackhas another depending on the snapshot))toghcipath any more - Indexing with e.g. tag generators should also improve because these programs will just skip quasiquoter part and index all user-defined functions.
- Ngôn ngữ chính
- Haskell
- Star
- 323
- Fork
- 87
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của haskell/alex
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
-
Is module DFS really needed?Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
-
UTF-8 and text 2.0Đang mởre: unicode
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
haskell/bytestring#730 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
seal-harness/seal-harness#399 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
add previously overlooked rulesĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
fmidue/codeworld-tasks#124 ·
-
allow haskell-src-exts-1.24Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
reflex-frp/reflex#563 ·