Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curl macros introduction (part 2)
08-10-2011, 03:19 AM, (This post was last modified: 08-10-2011, 04:28 AM by RobertShiplett.)
#1
Brick  Curl macros introduction (part 2)
Curl macro basics

A Curl macro is declared using a default macro that comes with the Curl language:

{define-macro }

which also has a more powerful version, {define-syntax } which we will look at later.
The Curl documentation states:

{define-macro [attributes] {macro-name pattern-template}
code-body
}


The default attribute is package, but public and library are also permitted.

The pattern template uses the types common to the macro {syntax-switch }. We will start with token and text.

The code body of the macro uses one or more {return } expressions and other expressions to produce the Curl source output of the macro which is then inserted into the program. The most important expression for that code block is likely to be the expression {expand-template }.
Here is a macro to insert a UNICODE ndash between two words:

Code:
{curl 7.0 package}

{curl-file-attributes character-encoding = "utf8"}
{package TIKI-MACROS,
   {compiler-directives careful? = true}
}

{def c-dash:char='\u2013'} || so-called ndash character usable in HTML as –
{def n-dash:String={String c-dash}}

{define-macro public { ndash ?t1:token, ?t2:text }
   {return
      {expand-template
         {value
            ?t1 & n-dash & ?t2
         }
      }
   }
}

In our example, the {value } expression is not needed, and is there for illustration only. A {do } expression might be used in a macro which is used to simplify a task. But the macro must return a value (try replacing the value with do in our example.) You should experiment by placing any string in quotes after our {value } expression in the macro, then after a {do } in place of that {value }.


A macro returns CurlSource to be inserted into the source file at the time that file is parsed (macro coding errors may crop up before applets are even running).


Our macro must be defined in a package outside of our source code which will use the macro. Here is that source code:

Code:
{curl 7.0 applet}
{curl-file-attributes character-encoding = "windows-latin-1"}
{applet manifest = "manifest.mcurl",
   {compiler-directives careful? = true}
}

{import  ndash  from TIKI-MACROS}

{def test:String = "this"}

{paragraph We en-dash { ndash test, "that"}}

And this is how our ndash-ed text items appear in a web page:

On the web page Curl Wrote:We en-dash this–that

Our macro has substituted the two hyphenated values into our source code.
Without the macro our code might have been

{paragraph We en-dash {value test} \u2013 that}

There are subtle differences: the latter requires a value expression and the macro version required a quoted string.

In our ndash macro, the {value } expression is not needed, and is there for illustration only. A {do } expression might be used in a macro which is used to simplify a task. But the macro must return a value (try replacing the value with do in our example.) You should experiment by placing any string in quotes after our {value } expression in the macro, then after a {do } in place of that {value }.

A macro returns CurlSource to be inserted into the source file at the time that file is parsed (macro coding errors may crop up before applets are even running).

Our {ndash } macro accepts first a token, and then consumes all remaining characters up to the first '}' - so it would be easier just to use two tokens, or two identifiers or two expressions, but here we illustrate the singular use of text as a syntax-switch pattern type. Our parameters are separated by a comma, but we could have been cute and used an ordinary keyboard hyphen - or nothing. The comma is the common delimiter to convey intent and facilitate documentation.
Robert Shiplett, Curlr
Fredericton NB

Canada


Possibly Related Threads...
Thread Author Replies Views Last Post
Brick Curl macros introduction (part 1) RobertShiplett 4 6,993 04-25-2015, 05:28 PM
Last Post: glennmcgrath
  server-side Curl issue : TocDocument RobertShiplett 3 5,691 02-09-2015, 02:11 PM
Last Post: Sumeraxe
  Curl with HTML widget + CintaNotes RobertShiplett 0 4,444 09-08-2014, 07:42 AM
Last Post: RobertShiplett
  Curl for the new Kodansh kanji books RobertShiplett 0 3,032 09-07-2014, 08:51 PM
Last Post: RobertShiplett
  Curl on Facebook - another page RobertShiplett 0 3,737 02-01-2014, 02:29 AM
Last Post: RobertShiplett
  Curl for CoffeeScript folks RobertShiplett 0 4,758 06-29-2013, 02:52 AM
Last Post: RobertShiplett
  Curl JavaScript bytecodes RobertShiplett 0 4,939 06-19-2013, 10:33 PM
Last Post: RobertShiplett
  jinja2 templates for Curl RobertShiplett 0 4,147 06-14-2013, 08:33 AM
Last Post: RobertShiplett
  Curl and QNX RobertShiplett 0 4,932 04-24-2013, 11:38 PM
Last Post: RobertShiplett
  WikizPad Curl personal wiki RobertShiplett 0 4,115 03-11-2013, 01:09 AM
Last Post: RobertShiplett
Forum Jump:


Users browsing this thread:
3 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('196')