How to write multiline strings in SML? - string

How can I write a string that spans multiple lines?
I tried to use the line continuation character \ at the end like many other languages, but it does not seem to work. The following:
val s = "line1\
line2";
generates an error:
/tmp/...:1: error: unexpected character l in \ ... \
/tmp/...:1: error: no matching quote found on this line
Not using any line continuation as follows generates a similar error:
val s = "line1
line2";
What's the correct way to write a multiline string?

You have to put the backslash at the beginning of the next lines as well :
print "this is\
\ a string on\
\ 3 lines\n";

If you're using SML/NJ and you don't care much about portability, there's a lesser known feature that could give you not only multiline strings, but also variable interpolation. SML/NJ supports an extensible quotation mechanism, which is described in more detail here: https://www.smlnj.org/doc/quote.html
Now, to solve your problem, we could define the following quotation parser. I'm calling it s to draw a parallel with Scala's s"..." syntax, which will become clearer in a few moments.
val s : string SMLofNJ.frag list -> string =
let
fun fold (SMLofNJ.QUOTE s, acc) = acc ^ s
| fold (SMLofNJ.ANTIQUOTE s, acc) = acc ^ s
in
List.foldl fold ""
end
Notice that it makes use of the SMLofNJ structure to access compiler-specific datatypes and values (frag, QUOTE and ANTIQUOTE).
We can now use the above like this (make sure you're using the command line option -Cparser.quotations=true):
$ sml -Cparser.quotations=true
- val a = "var";
val a = "var" : string
-
- val b = `this is a raw quotation using ^a`;
val b = [QUOTE "this is a raw quotation using ",ANTIQUOTE "var",QUOTE ""] :
string SMLofNJ.frag list
-
- val c = s`this is an interpolated string: ^a`;
val c = "this is an interpolated string: var" : string
-
- print c;
this is an interpolated string: varval it = () : unit
-
- val d = s`this
… is
… a
… multiline string: ^a`;
val d = "this\nis\na\nmultiline string: var" : string
-
- print d;
this
is
a
multiline string: varval it = () : unit
As you can see, using quotations in conjunction with a very simple custom parser (the s function), we can get multiline interpolated strings in SML/NJ.
In my personal library, I've defined it in a bit more structured way:
structure Strings =
struct
structure Interpolated =
struct
local
open SMLofNJ
in
(**
* Support for multiline, interpolated strings.
*
* NB: requires SML/NJ quotations: sml -Cparser.quotations=true.
*
* ```sml
* - open Strings.Interpolated;
* - val v = "val";
* val v = "val" : string
* - val j = s`{
* = "key": "^v"
* = }`;
* val j = "{\n \"key\": \"val\"\n}" : string
* ```
*)
val s : string frag list -> string =
let
fun fold (QUOTE s, acc) = acc ^ s
| fold (ANTIQUOTE s, acc) = acc ^ s
in
List.foldl fold ""
end
end
end
end

Related

LUA: Generating Unique Mac from given Number Value

I am trying to generate a unique MAC id from given a number value. The length on the number is between 1 to 5 digit. I have formatted the MAC table to place each digit starting from first value of MAC.
local MacFormat ={[1] = "0A:BC:DE:FA:BC:DE",[2] = "00:BC:DE:FA:BC:DE",[3] = "00:0C:DE:FA:BC:DE",[4] = "00:00:DE:FA:BC:DE",[5] = "00:00:0E:FA:BC:DE"}
local idNumbers = {[1] = "1",[2]="12",[3]="123",[4]="1234",[5]="12345"}
for w in string.gfind(idNumbers[3], "(%d)") do
print(w)
str = string.gsub(MacFormat[3],"0",tonumber(w))
end
print(str)
---output 33:3C:DE:FA:BC:DE
--- Desired Output 12:3C:DE:FA:BC:DE
I have tried multiple Patterns with *, +, ., but none is working.
for w in string.gfind(idNumbers[3], "(%d)") do
print(w)
str = string.gsub(MacFormat[3],"0",tonumber(w))
end
print(str)
Your loop body is equivalent to
str = string.gsub("00:0C:DE:FA:BC:DE", "0",1)
str = string.gsub("00:0C:DE:FA:BC:DE", "0", 2)
str = string.gsub("00:0C:DE:FA:BC:DE", "0", 3)
So str is "33:3C:DE:FA:BC:DE"
MacFormat[3] is never altered and the result of gsub is overwritten in each line.
You can build the pattern and replacement dynamically:
local MacFormat ={[1] = "0A:BC:DE:FA:BC:DE",[2] = "00:BC:DE:FA:BC:DE",[3] = "00:0C:DE:FA:BC:DE",[4] = "00:00:DE:FA:BC:DE",[5] = "00:00:0E:FA:BC:DE"}
local idNumbers = {[1] = "1",[2]="12",[3]="123",[4]="1234",[5]="12345"}
local p = "^" .. ("0"):rep(string.len(idNumbers[3])):gsub("(..)", "%1:")
local repl = idNumbers[3]:gsub("(..)", "%1:")
local str = MacFormat[3]:gsub(p, repl)
print(str)
-- => 12:3C:DE:FA:BC:DE
See the online Lua demo.
The pattern is "^" .. ("0"):rep(string.len(idNumbers[3])):gsub("(..)", "%1:"): ^ matches the start of string, then a string of zeros (of the same size a idNumbers, see ("0"):rep(string.len(idNumbers[3]))) follows with a : after each pair of zeros (:gsub("(..)", "%1:")).
The replacement is the idNumbers item with a colon inserted after every second char with idNumbers[3]:gsub("(..)", "%1:").
In this current case, the pattern will be ^00:0 and the replacement will be 12:3.
See the full demo here.

How to convert string to a table?

I have this string text:
text = "hotkey=F4,value=,autoSend=false, hotkey=Shift+F9,value=,autoSend=false, hotkey=F5,value=,autoSend=false"
and I would like to convert it to a table like this one:
local table = {
{hotkey='F4', value=nil, autoSend=false};
{hotkey='Shift+F9', value=nil, autoSend=false};
{hotkey='F5', value=nil, autoSend=false}
}
This solution is limited in scope and will not cover all complexities in the input string. A simple pattern matching could generate tables you are looking for, but use this code to build a better/robust regex for the diversity of your strings
s = "hotkey=F4,value=,autoSend=false, hotkey=Shift+F9,value=,autoSend=false, hotkey=F5,value=,autoSend=false"
local words = {}
for w in s:gmatch("(hotkey=%g-,value=%g-,autoSend=%w*)") do
-- Split string in more managebale parts
-- i-g w = 'hotkey=F4,value=,autoSend=false, hotkey=Shift+F9'
-- Extract indivisual k,v pairs and insert into table as desired
local _hotkey = string.match(w,"hotkey=(%g-),")
local _value = string.match(w,"value=(%g-),")
local _autoSend = string.match(w,"autoSend=(%w+)")
table.insert(words,{hotkey=_hotkey, value=_value, autoSend=_autoSend})
end
for _, w in ipairs(words) do
for k, v in pairs(w) do
print(k .. ':' .. v)
end
end
Regex Explanation
(): Capture string
%g: printable characters except for spaces
%w: alphanumeric characters
* : 0 or more repetitions
- : 0 or more lazy repetitions

String interpolation

In scala, you easily include the content of a variable inside a string, like this:
val nm = "Arrr"
println(s"my name is , $nm")
Is this possible in nim, and in that case, how?
The strfmt module features some experimental string interpolation:
import strfmt
let nm = "Arrr"
echo interp"my name is $nm"
Adding your own string interpolation is not particularly had, since the standard library already provides most of the necessary pieces:
import macros, parseutils, sequtils
macro i(text: string{lit}): expr =
var nodes: seq[PNimrodNode] = #[]
# Parse string literal into "stuff".
for k, v in text.strVal.interpolatedFragments:
if k == ikStr or k == ikDollar:
nodes.add(newLit(v))
else:
nodes.add(parseExpr("$(" & v & ")"))
# Fold individual nodes into a statement list.
result = newNimNode(nnkStmtList).add(
foldr(nodes, a.infix("&", b)))
const
multiplier = 3
message = i"$multiplier times 2.5 is ${multiplier * 2.5}"
echo message
# --> 3 times 2.5 is 7.5
proc blurb(a: int): string =
result = i"param a ($a) is not a constant"
when isMainModule:
for f in 1..10:
echo f.blurb
The strformat module is now considered the go-to way to do it:
import strformat
let nm = "Arrr"
echo fmt"my name is , {nm}"

How to concatenate strings into one using loop?

can someone help me with string concatenate problem. I read data from register. It's function utf(regAddr, length). I get table with decimal numbers, then I transform it into hex and to string in loop. I need concatenate these strings into one.
there is not in Lua something like .= operator
function utf(regAddr, length)
stringTable = {}
table.insert(stringTable, {mb:readregisters(regAddr-1,length)})
for key, value in pairs(stringTable) do
for i=1, length do
v = value[i]
v = lmcore.inttohex(v, 4)
v = cnv.hextostr(v)
log(v)
end
end
end
-- function(regAddr, length)
utf(30,20)
There is no append operator for strings. Strings are immutable values.
The .. operator concatenates two string, producing a third string as a result:
local b = "con"
local c = "catenate"
local a = b .. c -- "concatenate"
The table.concat function concatenates strings in a table, producing a string result:
local t = { "con", "catenate" }
local a = table.concat(t) -- "concatenate"
local t = { "two", "words" }
local a = table.concat(t, " ") -- "two words"
The string.format function takes a format pattern with a list of compatible values, producing a string result:
local b = 2
local c = "words"
local a = string.format("%i %s", b, c) -- "2 words"
local t = { 2, "words" }
local a = string.format("%i %s", unpack(t)) -- "2 words"
If you are accumulating a lot of strings that you eventually want to concatenate, you can use a table as a temporary data structure and concatenate when you are done accumulating:
local t = {}
for i = 1, 1000 do
table.insert(t, tostring(i))
end
local a = table.concat(t) -- "1234...9991000"
For a very large number of strings, you can concatenate incrementally. See LTN 9: Creating Strings Piece by Piece and related discussions.
You should try the table.concat method.
Maybe this other question can help you:
Lua table.concat
Checkout this tutorial http://lua-users.org/wiki/TableLibraryTutorial
this code works:
function utf(regAddr, length)
stringTable = {}
table.insert(stringTable, {mb:readregisters(regAddr-1,length)})
for key, value in pairs(stringTable) do
t = {}
for i=1, length do
v = value[i]
v = lmcore.inttohex(v, 4)
v = cnv.hextostr(v)
table.insert(t, v)
end
a = table.concat(t)
end
end
-- function(regAddr, length)
utf(30,20)

Optional capture of balanced brackets in Lua

Let's say I have lines of the form:
int[4] height
char c
char[50] userName
char[50+foo("bar")] userSchool
As you see, the bracketed expression is optional.
Can I parse these strings using Lua's string.match() ?
The following pattern works for lines that contain brackets:
line = "int[4] height"
print(line:match('^(%w+)(%b[])%s+(%w+)$'))
But is there a pattern that can handle also the optional brackets? The following does not work:
line = "char c"
print(line:match('^(%w+)(%b[]?)%s+(%w+)$'))
Can the pattern be written in another way to solve this?
Unlike regular expressions, ? in Lua pattern matches a single character.
You can use the or operator to do the job like this:
line:match('^(%w+)(%b[])%s+(%w+)$') or line:match('^(%w+)%s+(%w+)$')
A little problem with it is that Lua only keeps the first result in an expression. It depends on your needs, use an if statement or you can give the entire string the first capture like this
print(line:match('^((%w+)(%b[])%s+(%w+))$') or line:match('^((%w+)%s+(%w+))$'))
LPeg may be more appropriate for your case, especially if you plan to expand your grammar.
local re = require're'
local p = re.compile( [[
prog <- stmt* -> set
stmt <- S { type } S { name }
type <- name bexp ?
bexp <- '[' ([^][] / bexp)* ']'
name <- %w+
S <- %s*
]], {set = function(...)
local t, args = {}, {...}
for i=1, #args, 2 do t[args[i+1]] = args[i] end
return t
end})
local s = [[
int[4] height
char c
char[50] userName
char[50+foo("bar")] userSchool
]]
for k, v in pairs(p:match(s)) do print(k .. ' = ' .. v) end
--[[
c = char
userSchool = char[50+foo("bar")]
height = int[4]
userName = char[50]
--]]

Resources