Skip to content

Commit

Permalink
Add GHC Core lexer (#1377)
Browse files Browse the repository at this point in the history
This commit adds a lexer for the GHC Core language.
  • Loading branch information
supersven authored and pyrmont committed Dec 23, 2019
1 parent 2caa1a0 commit 78bc930
Show file tree
Hide file tree
Showing 4 changed files with 857 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/rouge/demos/ghc-core
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Rec {
-- RHS size: {terms: 24, types: 3, coercions: 0, joins: 0/0}
Main.fib_fib [Occ=LoopBreaker] :: Integer -> Integer
[GblId, Arity=1, Str=<S,U>, Unf=OtherCon []]
Main.fib_fib
= \ (ds_d2OU :: Integer) ->
case integer-gmp-1.0.2.0:GHC.Integer.Type.eqInteger#
ds_d2OU Main.fib1
of {
__DEFAULT ->
case integer-gmp-1.0.2.0:GHC.Integer.Type.eqInteger#
ds_d2OU Main.fib3
of {
__DEFAULT ->
integer-gmp-1.0.2.0:GHC.Integer.Type.plusInteger
(Main.fib_fib
(integer-gmp-1.0.2.0:GHC.Integer.Type.minusInteger
ds_d2OU Main.fib3))
(Main.fib_fib
(integer-gmp-1.0.2.0:GHC.Integer.Type.minusInteger
ds_d2OU Main.fib2));
1# -> Main.fib3
};
1# -> Main.fib1
}
end Rec }
151 changes: 151 additions & 0 deletions lib/rouge/lexers/ghc_core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

# See http://www.cs.cmu.edu/afs/andrew/course/15/411/ghc/share/doc/ghc/ext-core/core.pdf for a description of the syntax
# of the language and https://www.aosabook.org/en/ghc.html for a high level overview.
module Rouge
module Lexers
class GHCCore < RegexLexer
title "GHC Core"
desc "Intermediate representation of the GHC Haskell compiler."
tag 'ghc-core'

filenames '*.dump-simpl', '*.dump-cse', '*.dump-ds', '*.dump-spec'

state :root do
# sections
rule %r/^=====.*=====$/, Generic::Heading
# timestamps
rule %r/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ UTC$/, Comment::Single
rule %r/^Result size of .+\s*.*}/, Comment::Multiline
rule %r/--.*$/, Comment::Single

rule %r/\[/, Comment::Special, :annotation

mixin :recursive_binding
mixin :ghc_rule
mixin :function

# rest is Text
rule %r/\s/m, Text
rule %r/.*/, Text
end

state :expression do
rule %r/[\n]+/, Text
rule %r/(?=^\S)/ do
pop!
end

rule %r/\s+/, Text, :expression_line
end

state :expression_line do
rule %r/ /, Text

mixin :common

rule %r/\n/, Text, :pop!
end

state :annotation do
rule %r/\]/, Comment::Special, :pop!
rule %r/\[/, Comment::Special, :annotation
rule %r/[^\[\]]+/, Comment::Special
end

state :common do
# array, e.g. '[]' or '[Char]'
rule %r/\[[^=]*\]/, Keyword::Type

rule %r/\[/, Comment::Special, :annotation

mixin :literal
mixin :constants
mixin :punctuation
mixin :operator
mixin :name
end

state :literal do
rule %r/\d+\.\d+\#{0,2}/, Literal::Number::Float
rule %r/\d+\#{0,2}/, Literal::Number::Integer
rule %r/".*"#/, Literal::String
rule %r/'.'#/, Literal::String::Char
end

state :constants do
rule %r/__DEFAULT/, Name::Constant
end

state :name do
rule %r/^([A-Z]\w*)(\.)/ do |m|
token Name::Namespace, m[0]
token Punctuation, m[1]
end

rule %r/[A-Z][^\s.,(){}]*/, Keyword::Type

# packages, e.g. 'ghc-prim-0.5.3:'
rule %r/(^[a-z].*?\d+\.\d+\.\d+)(:)(?=\S)/ do |m|
token Name::Namespace, m[1]
token Punctuation, m[2]
end

rule %r/\S*\(,\)\S*/, Name::Variable # '(,)' is a name, not punctuation
rule %r/\S[^\s.,(){}]*/, Name::Variable
end

state :punctuation do
rule %r/[.,(){};]/, Punctuation
end

state :operator do
rule %r/=>/, Operator
rule %r/->/, Operator
rule %r/::/, Operator
rule %r/=/, Operator
rule %r/forall/, Keyword
rule %r/case/, Keyword
rule %r/of/, Keyword
rule %r/letrec/, Keyword
rule %r/let/, Keyword
rule %r/join/, Keyword
rule %r/@/, Operator
rule %r/\\/, Operator
end

state :recursive_binding do
rule %r/(Rec)(\s*)({)/ do |m|
token Keyword, m[1]
token Text, m[2]
token Punctuation, m[3]
end

rule %r/^(end)(\s*)(Rec)(\s*)(})/ do |m|
token Keyword, m[1]
token Text, m[2]
token Keyword, m[3]
token Text, m[4]
token Punctuation, m[5]
end
end

state :ghc_rule do
rule %r/^(".*?")/m do |m|
token Name::Label, m[0]

push :expression
end
end

state :function do
rule %r/^(\S+)(?=.*?(=|::))/m do |m|
token Name::Function, m[0]

push :expression
end
end
end
end
end
Loading

0 comments on commit 78bc930

Please sign in to comment.