Skip to content

Commit

Permalink
Update YAML #130
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdancondorachi committed May 28, 2024
1 parent ea8bb97 commit 7432aa2
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/Languages/Yaml/Patterns/YamlDateTimePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Yaml\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: 'date: 2024-05-31', output: '2024-05-31')]
#[PatternTest(input: 'time: 12:34:56', output: '12:34:56')]
#[PatternTest(input: 'date_time: 2024-05-31 12:34:56', output: '2024-05-31 12:34:56')]
final readonly class YamlDateTimePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>\d{4}-\d\d?-\d\d?(?:[tT]|[ \t]+)\d\d?:\d{2}:\d{2}(?:\.\d*)?(?:[ \t]*(?:Z|[-+]\d\d?(?::\d{2})?))?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(?::\d{2}(?:\.\d*)?)?)';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::NUMBER;
}
}
27 changes: 27 additions & 0 deletions src/Languages/Yaml/Patterns/YamlLogicalValuePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Yaml\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: 'required: true', output: 'true')]
#[PatternTest(input: 'required: TRUE', output: 'TRUE')]
final readonly class YamlLogicalValuePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?i)\b(?<match>(?:true|false|yes|no|null))\b';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::LITERAL;
}
}
26 changes: 26 additions & 0 deletions src/Languages/Yaml/Patterns/YamlNumberPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\Yaml\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest(input: 'age: 28', output: '28')]
final readonly class YamlNumberPattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<!\w)(?<match>[+-]?(?:0x[\da-fA-F]+|0o[0-7]+|(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|\.inf|\.nan))(?!\w)';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::NUMBER;
}
}
6 changes: 6 additions & 0 deletions src/Languages/Yaml/YamlLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use Tempest\Highlight\Languages\Yaml\Patterns\YamlColonPattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlCommentPattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlDashPattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlDateTimePattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlDoubleAccoladesValuePattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlDoubleQuoteValuePattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlLogicalValuePattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlNumberPattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlObjectBracketsPattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlPipePattern;
use Tempest\Highlight\Languages\Yaml\Patterns\YamlPropertyPattern;
Expand Down Expand Up @@ -44,8 +47,11 @@ public function getPatterns(): array
...parent::getPatterns(),
new YamlPropertyPattern(),
new YamlDashPattern(),
new YamlDateTimePattern(),
new YamlColonPattern(),
new YamlPipePattern(),
new YamlLogicalValuePattern(),
new YamlNumberPattern(),
new YamlVariablePattern(),
new YamlArrayBracketsPattern(),
new YamlObjectBracketsPattern(),
Expand Down
4 changes: 2 additions & 2 deletions tests/Languages/Yaml/YamlLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public static function data(): array
TXT,
<<<TXT
<span class="hl-keyword">php-version-stats-january-2024</span><span class="hl-property">:</span>
<span class="hl-keyword">date</span><span class="hl-property">:</span> 2024-01-29
<span class="hl-keyword">date</span><span class="hl-property">:</span> <span class="hl-number">2024-01-29</span>
<span class="hl-keyword">string</span><span class="hl-property">:</span> &quot;<span class="hl-value">PHP version stats: January, 2024</span>&quot;
<span class="hl-keyword">single-string</span><span class="hl-property">:</span> '<span class="hl-value">PHP version stats: January, 2024</span>'
<span class="hl-keyword">pipe</span><span class="hl-property">:</span> <span class="hl-property">|</span>
hello
world
<span class="hl-keyword">array</span><span class="hl-property">:</span> <span class="hl-property">[</span> true <span class="hl-property">]</span> <span class="hl-comment">#comment</span>
<span class="hl-keyword">array</span><span class="hl-property">:</span> <span class="hl-property">[</span> <span class="hl-literal">true</span> <span class="hl-property">]</span> <span class="hl-comment">#comment</span>
<span class="hl-keyword">object</span><span class="hl-property">:</span>
<span class="hl-property">-</span> <span class="hl-property">{</span> <span class="hl-keyword">title</span><span class="hl-property">:</span> &quot;<span class="hl-value">a</span>&quot;, <span class="hl-keyword">link</span>: &quot;<span class="hl-value">prop</span>&quot; <span class="hl-property">}</span>
<span class="hl-keyword">runs-on</span><span class="hl-property">:</span> $<span class="hl-value">{{</span><span class="hl-property"> matrix.os </span><span class="hl-value">}}</span>
Expand Down
53 changes: 53 additions & 0 deletions tests/targets/yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
```yaml
# Example YAML for syntax highlighting test

# Scalar values
name: John Doe
age: 30
is_student: false

# Sequence (list) of scalars
favorite_colors:
- blue
- green
- red

# Mapping (dictionary)
address:
street: 123 Main St
city: Anytown
postal_code: 12345

# Nested mappings
education:
high_school:
name: Anytown High School
graduation_year: 2010
university:
name: State University
graduation_year: 2014
degree: Computer Science

# Sequence of mappings
work_experience:
- company: ABC Corp
position: Software Engineer
start_date: 2015-06-01
end_date: 2018-08-30
- company: XYZ Inc
position: Senior Developer
start_date: 2018-09-01
end_date: present

# Multi-line string (literal block scalar)
bio: |
John Doe is a software developer with over 10 years of experience.
He has worked on a variety of projects, ranging from web development to
data analysis. In his free time, he enjoys hiking and photography.
# Multi-line string (folded block scalar)
quote: >
"The only limit to our realization of tomorrow
is our doubts of today."
- Franklin D. Roosevelt
```

0 comments on commit 7432aa2

Please sign in to comment.