Skip to content

Commit

Permalink
F#: Improved character literals (#1956)
Browse files Browse the repository at this point in the history
This adds support for all F# character escapes which should result in all characters being highlighted as such. 
The problem was that character literals with escape sequences longer than two characters were not recognized.
  • Loading branch information
RunDevelopment authored Jul 13, 2019
1 parent 11f18e3 commit d58d2ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion components/prism-fsharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Prism.languages.fsharp = Prism.languages.extend('clike', {
}
],
'string': {
pattern: /(?:"""[\s\S]*?"""|@"(?:""|[^"])*"|"(?:\\[\s\S]|[^\\"])*")B?|'(?:[^\\']|\\.)'B?/,
pattern: /(?:"""[\s\S]*?"""|@"(?:""|[^"])*"|"(?:\\[\s\S]|[^\\"])*")B?|'(?:[^\\']|\\(?:.|\d{3}|x[a-fA-F\d]{2}|u[a-fA-F\d]{4}|U[a-fA-F\d]{8}))'B?/,
greedy: true
},
'class-name': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-fsharp.min.js

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

29 changes: 14 additions & 15 deletions examples/prism-fsharp.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ <h2>Comments</h2>

<h2>Strings</h2>
<pre><code>"foo \"bar\" baz"
'foo \'bar\' baz'
@"Verbatim strings"
"""Alternate "verbatim" strings"""
</code></pre>
Expand Down Expand Up @@ -58,32 +57,32 @@ <h2>Numbers</h2>
</code></pre>

<h2>Full example</h2>
<pre><code>// The declaration creates a constructor that takes two values, name and age.
<pre><code>// The declaration creates a constructor that takes two values, name and age.
type Person(name:string, age:int) =
// A Person object's age can be changed. The mutable keyword in the
// declaration makes that possible.
// A Person object's age can be changed. The mutable keyword in the
// declaration makes that possible.
let mutable internalAge = age

// Declare a second constructor that takes only one argument, a name.
// This constructor calls the constructor that requires two arguments,
// sending 0 as the value for age.
// Declare a second constructor that takes only one argument, a name.
// This constructor calls the constructor that requires two arguments,
// sending 0 as the value for age.
new(name:string) = Person(name, 0)

// A read-only property.
// A read-only property.
member this.Name = name
// A read/write property.
// A read/write property.
member this.Age
with get() = internalAge
and set(value) = internalAge &lt;- value

// Instance methods.
// Increment the person's age.
// Instance methods.
// Increment the person's age.
member this.HasABirthday () = internalAge &lt;- internalAge + 1

// Check current age against some threshold.
// Check current age against some threshold.
member this.IsOfAge targetAge = internalAge &gt;= targetAge

// Display the person's name and age.
override this.ToString () =
// Display the person's name and age.
override this.ToString () =
"Name: " + name + "\n" + "Age: " + (string)internalAge
</code></pre>
</code></pre>
12 changes: 10 additions & 2 deletions tests/languages/fsharp/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ bar"""
'a'B
'\''
'\\'
'\231'
'\x41'
'\u0041'
'\U0001F47D'

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

Expand All @@ -39,9 +43,13 @@ bar"""
["string", "'a'"],
["string", "'a'B"],
["string", "'\\''"],
["string", "'\\\\'"]
["string", "'\\\\'"],
["string", "'\\231'"],
["string", "'\\x41'"],
["string", "'\\u0041'"],
["string", "'\\U0001F47D'"]
]

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

Checks for normal strings, verbatim strings, triple-quoted strings and character literals.
Checks for normal strings, verbatim strings, triple-quoted strings and character literals.

0 comments on commit d58d2ae

Please sign in to comment.