YAML - Syntax Primitives

In this chapter you will learn about the following aspects of syntax primitives in YAML −
Production parameters
Indentation Spaces
Separation Spaces
Ignored Line Prefix
Line folding
Let us understand each aspect in detail.
Production parameters include a set of parameters and the range of allowed values which are used on a specific production. The following list of production parameters are used in YAML −
It is denoted by character n or m	Character stream depends on the indentation level of blocks included in it. Many productions have parameterized these features.
It is denoted by c.	YAML supports two groups of contexts: block styles and flow styles.
It is denoted by s. Scalar content may be presented in one of the five styles: plain, double quoted and single quoted flow, literal and folded block.
It is denoted by t. Block scalars offer many mechanisms which help in trimming the block: strip, clip and keep. Chomping helps in formatting new line strings. It is used Block style representation. Chomping process happens with the help of indicators. The indicators controls what output should be produced with newlines of string. The newlines are removed with (-) operator and newlines are added with (+) operator.
An example for chomping process is shown below −
strip: |-
   text↓
clip: |
   text↓
keep: |+
   text↓

The output after parsing the specified YAML example is as follows −
In YAML character stream, indentation is defined as a line break character by zero or more characters. The most important point to be kept in mind is that indentation must not contain any tab characters. The characters in indentation should never be considered as a part of node’s content information. Observe the following code for better understanding −
%YAML 1.1
---
!!map {
   ? !!str "Not indented"
   : !!map {
      ? !!str "By one space"
      : !!str "By four\n spaces\n",
      ? !!str "Flow style"
      : !!seq [
         !!str "By two",
         !!str "Still by two",
         !!str "Again by two",
      ]
   }
}
The output that you can see after indentation is as follows −
{
   "Not indented": {
      "By one space": "By four\n spaces\n", 
      "Flow style": [
         "By two", 
         "Still by two", 
         "Again by two"
      ]
   }
}

YAML uses space characters for separation between tokens. The most important note is that separation in YAML should not contain tab characters.
The following lone of code shows the usage of separation spaces −
{ · first: · Sammy, · last: · Sosa · }
{
   "\u00b7 last": "\u00b7 Sosa \u00b7", 
   "\u00b7 first": "\u00b7 Sammy"
}

Empty prefix always includes indentation depending on the scalar type which also includes a leading whitespace. Plain scalars should not contain any tab characters. On the other hand, quoted scalars may contain tab characters. Block scalars completely depend on indentation.
The following example shows the working of ignored line prefix in a systematic manner −
%YAML 1.1
---
!!map {
   ? !!str "plain"
   : !!str "text lines",
   ? !!str "quoted"
   : !!str "text lines",
   ? !!str "block"
   : !!str "text·®lines\n"
}
The output achieved for the block streams is as follows −
{
   "plain": "text lines", 
   "quoted": "text lines", 
   "block": "text\u00b7\u00aelines\n"
}

Line Folding allows breaking long lines for readability. More amounts of short lines mean better readability. Line folding is achieved by noting original semantics of long line. The following example demonstrates line folding −
%YAML 1.1
--- !!str
"specific\L\
trimmed\n\n\n\
as space"
You can see the output for line folding in JSON format as follows −
"specific\u2028trimmed\n\n\nas space"


    33 Lectures 
     44 mins

 Tarun Telang
 Print
 Add Notes
 Bookmark this page