Skip to content

Commit

Permalink
Merge pull request #803 from Golmote/prism-icon
Browse files Browse the repository at this point in the history
Add support for Icon
  • Loading branch information
Golmote committed Oct 17, 2015
2 parents 253c035 + 2d08fa9 commit b43c5f3
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ var components = {
"title": "HTTP",
"owner": "danielgtaylor"
},
"icon": {
"title": "Icon",
"owner": "Golmote"
},
"inform7": {
"title": "Inform 7",
"owner": "Golmote"
Expand Down
17 changes: 17 additions & 0 deletions components/prism-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Prism.languages.icon = {
'comment': /#.*/,
'string': /(["'])(?:(?!\1)[^\\\r\n]|\\.|_(?:\r?\n|\r))*\1/,
'number': /\b(?:\d+r[a-z\d]+|\d+(?:\.\d+)?(?:e[+-]?\d+)?)\b|\.\d+\b/i,
'builtin-keyword': {
pattern: /&(?:allocated|ascii|clock|collections|cset|current|date|dateline|digits|dump|e|error(?:number|text|value)?|errout|fail|features|file|host|input|lcase|letters|level|line|main|null|output|phi|pi|pos|progname|random|regions|source|storage|subject|time|trace|ucase|version)\b/,
alias: 'variable'
},
'directive': {
pattern: /\$\w+/,
alias: 'builtin'
},
'keyword': /\b(?:break|by|case|create|default|do|else|end|every|fail|global|if|initial|invocable|link|local|next|not|of|procedure|record|repeat|return|static|suspend|then|to|until|while)\b/,
'function': /(?!\d)\w+(?=\s*[({]|\s*!\s*\[)/,
'operator': /[+-]:(?!=)|(?:[\/?@^%&]|\+\+?|--?|==?=?|~==?=?|\*\*?|\|\|\|?|<(?:->?|<?=?)|>>?=?)(?::=)?|:(?:=:?)?|[!.\\|~]/,
'punctuation': /[\[\](){},;]/
};
1 change: 1 addition & 0 deletions components/prism-icon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

186 changes: 186 additions & 0 deletions examples/prism-icon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<h1>Icon</h1>
<p>To use this language, use the class "language-icon".</p>

<h2>Comments</h2>
<pre><code>#
# Foobar</code></pre>

<h2>Strings and csets</h2>
<pre><code>""
"Foo\"bar"
''
'a\'bcdefg'</code></pre>

<h2>Numbers</h2>
<pre><code>42
3.14159
5.2E+8
16rface
2r1101</code></pre>

<h2>Full example</h2>
<pre><code># Author: Robert J. Alexander
global GameObject, Tree, Learn
record Question(question, yes, no)
procedure main()
GameObject := "animal"
Tree := Question("Does it live in water", "goldfish", "canary")
Get() # Recall prior knowledge
Game() # Play a game
return
end
# Game() -- Conducts a game.
#
procedure Game()
while Confirm("Are you thinking of ", Article(GameObject), " ",
GameObject) do Ask(Tree)
write("Thanks for a great game.")
if \Learn &Confirm("Want to save knowledge learned this session")
then Save()
return
end
# Confirm() -- Handles yes/no questions and answers.
#
procedure Confirm(q[])
local answer, s
static ok
initial {
ok := table()
every ok["y" | "yes" | "yeah" | "uh huh"] := "yes"
every ok["n" | "no" | "nope" | "uh uh" ] := "no"
}
while /answer do {
every writes(!q)
write("?")
case s := read() | exit(1) of {
# Commands recognized at a yes/no prompt.
#
"save": Save()
"get": Get()
"list": List()
"dump": Output(Tree)
default: {
(answer := \ok[map(s, &ucase, &lcase)]) |
write("This is a \"yes\" or \"no\" question.")
}
}
}
return answer == "yes"
end
# Ask() -- Navigates through the barrage of questions leading to a
# guess.
#
procedure Ask(node)
local guess, question
case type(node) of {
"string": {
if not Confirm("It must be ", Article(node), " ", node, ", right") then {
Learn := "yes"
write("What were you thinking of?")
guess := read() | exit(1)
write("What question would distinguish ", Article(guess), " ",
guess, " from ", Article(node), " ", node, "?")
question := read() | exit(1)
if question[-1] == "?" then question[-1] := ""
question[1] := map(question[1], &lcase, &ucase)
if Confirm("For ", Article(guess), " ", guess, ", what would the answer be")
then return Question(question, guess, node)
else return Question(question, node, guess)
}
}
"Question": {
if Confirm(node.question) then node.yes := Ask(node.yes)
else node.no := Ask(node.no)
}
}
end
# Article() -- Come up with the appropriate indefinite article.
#
procedure Article(word)
return if any('aeiouAEIOU', word) then "an" else "a"
end
# Save() -- Store our acquired knowledge in a disk file name
# based on the GameObject.
#
procedure Save()
local f
f := open(GameObject || "s", "w")
Output(Tree, f)
close(f)
return
end
# Output() -- Recursive procedure used to output the knowledge tree.
#
procedure Output(node, f, sense)
static indent
initial indent := 0
/f := &output
/sense := " "
case type(node) of {
"string": write(f, repl(" ", indent), sense, "A: ", node)
"Question": {
write(f, repl(" ", indent), sense, "Q: ", node.question)
indent +:= 1
Output(node.yes, f, "y")
Output(node.no, f, "n")
indent -:= 1
}
}
return
end
# Get() -- Read in a knowledge base from a file.
#
procedure Get()
local f
f := open(GameObject || "s", "r") | fail
Tree := Input(f)
close(f)
return
end
# Input() -- Recursive procedure used to input the knowledge tree.
#
procedure Input(f)
local nodetype, s
read(f) ? (tab(upto(~' \t')) & =("y" | "n" | "") &
nodetype := move(1) & move(2) & s := tab(0))
return if nodetype == "Q" then Question(s, Input(f), Input(f)) else s
end
# List() -- Lists the objects in the knowledge base.
#
$define Length 78
procedure List()
local lst, line, item
lst := Show(Tree, [ ])
line := ""
every item := !sort(lst) do {
if *line + *item > Length then {
write(trim(line))
line := ""
}
line ||:= item || ", "
}
write(line[1:-2])
return
end
#
# Show() -- Recursive procedure used to navigate the knowledge tree.
#
procedure Show(node, lst)
if type(node) == "Question" then {
lst := Show(node.yes, lst)
lst := Show(node.no, lst)
}
else put(lst, node)
return lst
end</code></pre>

<h2>Known failures</h2>
<p>There are certain edge cases where Prism will fail.
There are always such cases in every regex-based syntax highlighter.
However, Prism dares to be open and honest about them.
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
</p>

<h3>Comment-like substrings and csets containing a hash</h3>
<pre><code>"foo # bar";
'abc#def'</code></pre>
91 changes: 91 additions & 0 deletions tests/languages/icon/builtin-keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
&allocated
&ascii
&clock
&collections
&cset
&current
&date
&dateline
&digits
&dump
&e
&error
&errornumber
&errortext
&errorvalue
&errout
&fail
&features
&file
&host
&input
&lcase
&letters
&level
&line
&main
&null
&output
&phi
&pi
&pos
&progname
&random
&regions
&source
&storage
&subject
&time
&trace
&ucase
&version

----------------------------------------------------

[
["builtin-keyword", "&allocated"],
["builtin-keyword", "&ascii"],
["builtin-keyword", "&clock"],
["builtin-keyword", "&collections"],
["builtin-keyword", "&cset"],
["builtin-keyword", "&current"],
["builtin-keyword", "&date"],
["builtin-keyword", "&dateline"],
["builtin-keyword", "&digits"],
["builtin-keyword", "&dump"],
["builtin-keyword", "&e"],
["builtin-keyword", "&error"],
["builtin-keyword", "&errornumber"],
["builtin-keyword", "&errortext"],
["builtin-keyword", "&errorvalue"],
["builtin-keyword", "&errout"],
["builtin-keyword", "&fail"],
["builtin-keyword", "&features"],
["builtin-keyword", "&file"],
["builtin-keyword", "&host"],
["builtin-keyword", "&input"],
["builtin-keyword", "&lcase"],
["builtin-keyword", "&letters"],
["builtin-keyword", "&level"],
["builtin-keyword", "&line"],
["builtin-keyword", "&main"],
["builtin-keyword", "&null"],
["builtin-keyword", "&output"],
["builtin-keyword", "&phi"],
["builtin-keyword", "&pi"],
["builtin-keyword", "&pos"],
["builtin-keyword", "&progname"],
["builtin-keyword", "&random"],
["builtin-keyword", "&regions"],
["builtin-keyword", "&source"],
["builtin-keyword", "&storage"],
["builtin-keyword", "&subject"],
["builtin-keyword", "&time"],
["builtin-keyword", "&trace"],
["builtin-keyword", "&ucase"],
["builtin-keyword", "&version"]
]

----------------------------------------------------

Checks for builtin keywords.
13 changes: 13 additions & 0 deletions tests/languages/icon/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Foobar

----------------------------------------------------

[
["comment", "#"],
["comment", "# Foobar"]
]

----------------------------------------------------

Checks for comments.
21 changes: 21 additions & 0 deletions tests/languages/icon/directive_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$include
$line
$define
$undef
$ifdef
$ifndef

----------------------------------------------------

[
["directive", "$include"],
["directive", "$line"],
["directive", "$define"],
["directive", "$undef"],
["directive", "$ifdef"],
["directive", "$ifndef"]
]

----------------------------------------------------

Checks for preprocessor directives.
15 changes: 15 additions & 0 deletions tests/languages/icon/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
foo()
Foobar_42{}
Foo_Bar ! []

----------------------------------------------------

[
["function", "foo"], ["punctuation", "("], ["punctuation", ")"],
["function", "Foobar_42"], ["punctuation", "{"], ["punctuation", "}"],
["function", "Foo_Bar"], ["operator", "!"], ["punctuation", "["], ["punctuation", "]"]
]

----------------------------------------------------

Checks for functions.
Loading

0 comments on commit b43c5f3

Please sign in to comment.