English 中文(简体)
YAML – Syntax Primitives
  • 时间:2024-12-27

YAML - Syntax Primitives


Previous Page Next Page  

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

Production parameters include a set of parameters and the range of allowed values which are used on a specific production. The following pst of production parameters are used in YAML −

Indentation

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.

Context

It is denoted by c. YAML supports two groups of contexts: block styles and flow styles.

Style

It is denoted by s. Scalar content may be presented in one of the five styles: plain, double quoted and single quoted flow, pteral and folded block.

Chomping

It is denoted by t. Block scalars offer many mechanisms which help in trimming the block: strip, cpp and keep. Chomping helps in formatting new pne strings. It is used Block style representation. Chomping process happens with the help of indicators. The indicators controls what output should be produced with newpnes of string. The newpnes are removed with (-) operator and newpnes are added with (+) operator.

An example for chomping process is shown below −

strip: |-
   text↓
cpp: |
   text↓
keep: |+
   text↓

The output after parsing the specified YAML example is as follows −

Output After Parsing the Specified YAML

Indentation Spaces

In YAML character stream, indentation is defined as a pne 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
 spaces
",
      ? !!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
 spaces
", 
      "Flow style": [
         "By two", 
         "Still by two", 
         "Again by two"
      ]
   }
}

Separation Spaces

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 · }
The syntax shown above gives you the following output:
{
   "u00b7 last": "u00b7 Sosa u00b7", 
   "u00b7 first": "u00b7 Sammy"
}

Ignored Line Prefix

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 pne prefix in a systematic manner −

%YAML 1.1
---
!!map {
   ? !!str "plain"
   : !!str "text pnes",
   ? !!str "quoted"
   : !!str "text pnes",
   ? !!str "block"
   : !!str "text·®pnes
"
}

The output achieved for the block streams is as follows −

{
   "plain": "text pnes", 
   "quoted": "text pnes", 
   "block": "textu00b7u00aepnes
"
}

Line Folding

Line Folding allows breaking long pnes for readabipty. More amounts of short pnes mean better readabipty. Line folding is achieved by noting original semantics of long pne. The following example demonstrates pne folding −

%YAML 1.1
--- !!str
"specificL
trimmed



as space"

You can see the output for pne folding in JSON format as follows −

"specificu2028trimmed


as space"
Advertisements