Define the language pattern of a Universal Analyzer language
Overview
The xxxLanguagePattern.xml file describes how to find the objects of your language in source code, using regular expressions. It defines:
- where the language starts and stops in a file, when several languages are mixed in one file
- comments, strings, keywords, operators, numbers and identifiers
- for each object type, how to recognize its header, its properties and its body
- how to recognize links
- where embedded SQL statements start and stop
Use only the object types, categories and properties defined in the metamodel. The language pattern is used only during the analysis: it is not saved in the analysis schema, so keep a copy of it in your source repository.
File name and location
Name the file after the language category defined in the metamodel, followed by LanguagePattern.xml, for example PHPLanguagePattern.xml. Save it in the language package folder, next to the metamodel. If the name or location is wrong, the source files are not analyzed.
Writing patterns
Patterns are regular expressions in Perl syntax. In the reference tables below:
- an element name followed by
?can occur zero or one time - an element name followed by
*can occur zero or more times - an element name followed by
+can occur one or more times - an element name with no suffix is mandatory
Escape characters that are special in regular expressions with a backslash: for example, ? becomes \?. When a pattern contains characters that are not valid in XML, such as < or &, wrap it in a CDATA section. See Migrate an older language package for regular expression rules that changed between releases.
Example
The following skeleton shows how the elements fit together, for a PHP-like language with functions and class inheritance:
<?xml version="1.0" encoding="utf-8"?>
<languagePattern id="PHP">
<begin><![CDATA[<\?]]></begin>
<end><![CDATA[\?>]]></end>
<comment>
<begin><![CDATA[#]]></begin>
<end><![CDATA[\r\n]]></end>
</comment>
<comment>
<begin><![CDATA[/\*]]></begin>
<end><![CDATA[\*/]]></end>
<multiline>true</multiline>
</comment>
<string>
<begin><![CDATA["]]></begin>
<end><![CDATA["]]></end>
<escape><![CDATA[\]]></escape>
</string>
<keyword>function</keyword>
<keyword>class</keyword>
<keyword>extends</keyword>
<Types>
<phpFunction>
<header>
<pattern><![CDATA[[fF][uU][nN][cC][tT][iI][oO][nN]([ ]|[\r\n]|[\t])+]]></pattern>
</header>
<identification.name>
<pattern><![CDATA[[fF][uU][nN][cC][tT][iI][oO][nN]([ ]|[\r\n]|[\t])+]]></pattern>
<value><![CDATA[[[:word:]][[:word:]]*]]></value>
</identification.name>
<body>
<begin><![CDATA[\{]]></begin>
<end><![CDATA[\}]]></end>
</body>
</phpFunction>
</Types>
<Links>
<inheritLink>
<pattern><![CDATA[extends([ ]|[\r\n]|[\t])+]]></pattern>
<callee><![CDATA[[[:alpha:]_][[:alnum:]_]*]]></callee>
</inheritLink>
</Links>
</languagePattern>
Main elements
| Element | Children | Value | Description |
|---|---|---|---|
languagePattern |
All elements below | - | The root element. |
begin? |
- | Regular expression | The characters that start the language, for example <\? for PHP. |
end? |
- | Regular expression | The characters that end the language, for example \?>. |
escape? |
- | Character | The escape character for all elements of the language. |
comment* |
begin, end, nested?, multiline? |
- | A type of comment. Define comments if you want them to be counted. |
comment/begin |
- | Regular expression | The start of the comment, for example #. |
comment/end |
- | Regular expression | The end of the comment, for example \r\n. |
comment/nested? |
- | Boolean | Whether comments of this type can be nested. Default false. |
comment/multiline? |
- | Boolean | Whether the comment can span several lines. Default false. |
string* |
begin, end, escape |
- | A type of string. Used for word-based metrics such as copy-paste and Halstead metrics. |
keyword* |
- | String | A reserved keyword of the language, for example protected. Used for word-based metrics. |
operator* |
- | Regular expression | Tokens that are arithmetic or logical operators. Used for word-based metrics. |
numeric |
- | Regular expression | Tokens that are numeric literals, for example [0-9]+. Used for word-based metrics. |
identifier |
- | Regular expression | Tokens that are identifiers. Used for Halstead metrics. |
Types? |
One element per object type | - | Contains one element for each object type, named after the type in the metamodel. See Object types. |
Links? |
One element per link type | - | Contains one element for each link type, named after the link type. See Links. |
Keywords are literal strings, not regular expressions. Escape the characters ( ) [ ] ? * - + with a backslash: for example, the keyword list() is written <keyword>list\(\)</keyword>.
Object types
Each object type element, for example phpFunction, can contain the following elements:
| Element | Children | Value | Description |
|---|---|---|---|
header |
pattern, begin?, end? |
- | Recognizes the header of the object. |
header/pattern |
- | Regular expression | The anchor of the header, for example the keyword function. |
header/begin? |
- | Regular expression | The start of the header. |
header/end? |
- | Regular expression | The end of the header. |
category.property |
pattern, value |
- | Recognizes a property of the object. The element is named after the category and the property, for example identification.name. |
category.property/pattern |
- | Regular expression | The anchor of the property. |
category.property/value |
backward? |
Regular expression | The value of the property. |
body |
begin, end, nested?, noendpattern? |
- | Recognizes the body of the object. |
body/begin |
- | Regular expression | The start of the body, for example \{. |
body/end |
- | Regular expression | The end of the body, for example \}. |
body/nested? |
- | Boolean | Whether the start and end characters can appear inside the body. Default true. |
noendpattern |
- | Empty | The object has no end pattern. Use it in the type element or in body. |
endwithoutbody |
- | Regular expression | The end of an object that has no body, for example ;. |
For an object with no end pattern whose siblings are recognized by a property, see Special cases.
Backward matching
By default, a property value or a link callee is searched after the anchor pattern. When the value comes before the anchor, add an empty backward element to value or callee to reverse the search direction.
For example, in a language where a function name comes before the keyword function:
lng_my_function function
{
}
The name of the function is searched backward from the anchor:
<lngFunction>
<header>...</header>
<identification.name>
<pattern>function</pattern>
<value>
<![CDATA[([a-z_])+]]>
<backward/>
</value>
</identification.name>
<body>...</body>
</lngFunction>
Links
Each link type element, for example inheritLink, contains:
| Element | Children | Value | Description |
|---|---|---|---|
pattern |
- | Regular expression | The anchor of the link, for example extends followed by white space. |
callee |
backward? |
Regular expression | The name of the object that is called. Add an empty backward element to search before the anchor. |
The object types that can be callers and callees of each link type are declared in the metamodel.
Embedded SQL
If the language contains embedded SQL, define where each SQL statement starts and ends in an esql element. Replace BEGIN_ESQL and END_ESQL with the start and end markers used in your language:
<esql>
<begin><![CDATA[BEGIN_ESQL]]></begin>
<end><![CDATA[END_ESQL]]></end>
</esql>
Add one esql element for each pair of markers:
<esql>
<begin><![CDATA[BEGIN_ESQL_1]]></begin>
<end><![CDATA[END_ESQL_1]]></end>
</esql>
<esql>
<begin><![CDATA[BEGIN_ESQL_2]]></begin>
<end><![CDATA[END_ESQL_2]]></end>
</esql>
The object types that contain embedded SQL must also inherit from ESQLSearchable in the metamodel.