In this video, we'll cover SI Script. It's the alternative to string literals, the tag format we discussed in Part 2.
SI Script is an implementation of what's called ECMAScript. This is a W3C scripting language that you probably best know as JavaScript. The information inside the tags is ECMAScript. This gives you powerful, flexible commands because you have this whole scripting language available to you.
In order to use SI Script in your grammars, you need a tag-format declaration: semantics/1.0.2006. Like our declaration for string literals, this is a little different than what the W3 spec would say, because we maintain backwards compatibility with older versions of the SISR spec.
The main thing we want to do is access rule variables. The first thing we want to do is access the rule variable for the rule you're currently in. The current rule variable is identified by "out". What you'll be doing in SI Script is assigning a value to "out". Unlike in string variables, rule variables will always be initialized as empty objects. With SI Script as in JavaScript, you can declare variables and properties on the fly. You can modify the property of a rule variable by out.propertyname.
I'm sure you're familiar with this example by now, but we've made two changes here. The first is we've added our tag-format declaration at the top, and the second is that we've done some SISR using SI Script. We have curly braces for our SI tag: {out=100}.
#ABNF 1.0 UTF-8; language en-US; mode voice; tag-format; root $name; &robertsmith = (Robert|bob) [smith] {out=100}; $name = $robertsmith;
$robertsmith = (robert | bob) [smith]
{out.extension = 100; out.name = "Robert Smith"};
Only the rule variables of a rule's children are visible to that rule. Grandchildren are not visible to the grandparent. For example:
$ruleA = $ruleB;
To access another rule's variable, that rule must be visible to the current rule.
rules.rulename
rules.latest
root $date;
$date = $month $day $year;
$month = January {out = "01/"} | February
{out = "02/"} | March {out = "03/"};
$day = first {out = "01/"} | second
{out = "02/"} | third {out = "03/"};
$year = Two thousand one {out = "2001"} |
Two thousand two {out = "2002"} |
Two thousand three {out = "2003"};
So now when the caller says "January First Two Thousand One", the result is "01/01/2001".
That was using the rules.rulename method. When would you want to use the rules.latest method?
There are some instances when you can't be sure what order rules were spoken in. In the last example we were only going to accept month/ day /year. But let's say you want to collect a string of digits, and you don't know which digits are going to come first.
root &binary;
$binary = {out = ''} (($zero | $one)
{out + = rules.latest()})<1->;
$zero = (zero | o) {out = 0};
$one = one {out = 1};
When the caller says "Zero one zero one", the result is "0101".
In the next video, we'll cover advanced SI Script, specifically adding application-type logic to your grammar through SISR.