Browse Source

WIP: Modularize out parsers

feature/node-rewrite
Sven Slootweg 2 months ago
parent
commit
6b5bd204d3
10 changed files with 97 additions and 38 deletions
  1. +12
    -7
      src/packages/exec-smartctl/parsers/commands/attributes.pegjs
  2. +8
    -3
      src/packages/exec-smartctl/parsers/commands/info.pegjs
  3. +3
    -1
      src/packages/exec-smartctl/parsers/commands/scan.pegjs
  4. +4
    -26
      src/packages/exec-smartctl/parsers/primitives.pegjs
  5. +3
    -1
      src/packages/exec-smartctl/parsers/shared.pegjs
  6. +29
    -0
      src/packages/peg-grouped-number/index.pegjs
  7. +4
    -0
      src/packages/peg-hex-number/index.pegjs
  8. +3
    -0
      src/packages/peg-newline/index.pegjs
  9. +14
    -0
      src/packages/peg-number/index.pegjs
  10. +17
    -0
      src/packages/peg-whitespace/index.pegjs

+ 12
- 7
src/packages/exec-smartctl/parsers/commands/attributes.pegjs View File

@@ -1,4 +1,9 @@
import { _, RestOfLine, Newline, NumberValue, HexNumberValue, IdentifierValue } from "../primitives"
import { Integer } from "../../../peg-number"
import { HexInteger } from "../../../peg-hex-number"
import { Newline } from "../../../peg-newline"
import { SameLine as _ } from "../../../peg-whitespace"

import { RestOfLine, IdentifierValue } from "../primitives"
import { Header } from "../shared"

{
@@ -17,7 +22,7 @@ AttributesSection

AttributesSectionSATA
= "=== START OF READ SMART DATA SECTION ===" Newline
"SMART Attributes Data Structure revision number:" _ NumberValue Newline
"SMART Attributes Data Structure revision number:" _ Integer Newline
"Vendor Specific SMART Attributes with Thresholds:" Newline
"ID#" _ "ATTRIBUTE_NAME" _ "FLAG" _ "VALUE" _ "WORST" _ "THRESH" _ "TYPE" _ "UPDATED" _ "WHEN_FAILED" _ "RAW_VALUE" Newline
attributes:AttributeFieldSATA+ {
@@ -32,7 +37,7 @@ AttributesSectionNVMe
}

AttributeFlags
= "0x" number:HexNumberValue {
= "0x" number:HexInteger {
return mapAttributeFlags(number);
}

@@ -50,12 +55,12 @@ AttributeFieldType
/ "Old_age"

AttributeFieldSATA
= _ id:NumberValue
= _? id:Integer
_ attributeName:IdentifierValue
_ flags:AttributeFlags
_ value:NumberValue
_ worstValue:NumberValue
_ threshold:NumberValue
_ value:Integer
_ worstValue:Integer
_ threshold:Integer
_ type:AttributeFieldType
_ updatedWhen:AttributeUpdatedWhen
_ failedWhen:AttributeFailedWhen


+ 8
- 3
src/packages/exec-smartctl/parsers/commands/info.pegjs View File

@@ -1,4 +1,9 @@
import { _, RestOfLine, Newline, NumberValue, SeparatedNumberValue, BytesValue } from "../primitives"
import { Integer } from "../../../peg-number"
import { CommaDelimitedInteger as GroupedNumber } from "../../../peg-grouped-number"
import { Newline } from "../../../peg-newline"
import { SameLine as _ } from "../../../peg-whitespace"

import { RestOfLine, BytesValue } from "../primitives"
import { Header } from "../shared"

{
@@ -61,7 +66,7 @@ InfoFieldIgnored

InfoFieldSize
// NOTE: We don't actually care about the human-friendly display size after the 'bytes' specifier, hence the RestOfLine
= InfoFieldSizeKey _ value:SeparatedNumberValue _ "bytes"? _ RestOfLine {
= InfoFieldSizeKey _ value:GroupedNumber (_ "bytes")? _ RestOfLine {
return {
key: "size",
value: B(value)
@@ -73,7 +78,7 @@ InfoFieldSizeKey
/ "Total NVM Capacity:"

InfoFieldRPM
= "Rotation Rate:" _ value:NumberValue _ "rpm" Newline {
= "Rotation Rate:" _ value:Integer _ "rpm" Newline {
return {
key: "rpm",
value: value


+ 3
- 1
src/packages/exec-smartctl/parsers/commands/scan.pegjs View File

@@ -1,4 +1,6 @@
import { _, RestOfLine } from "../primitives"
import { SameLine as _ } from "../../../peg-whitespace"

import { RestOfLine } from "../primitives"

RootScan
= devices:ScanDevice* {


+ 4
- 26
src/packages/exec-smartctl/parsers/primitives.pegjs View File

@@ -1,42 +1,20 @@
import { Newline } from "../../peg-newline"
import { CommaDelimitedInteger as GroupedInteger } from "../../peg-grouped-number"

{
const syncpipe = require("syncpipe");
const {B} = require("../../unit-bytes-iec");
}

_
= (" " / "\t")*

RestOfLine
= content:$[^\n]+ Newline {
return content;
}

Newline
= "\n"
/ "\r\n"

BytesValue
= value:SeparatedNumberValue {
= value:GroupedInteger {
return B(value);
}
NumberValue
= value:$[0-9]+ {
return parseInt(value);
}

SeparatedNumberValue
= value:$[0-9,]+ {
return syncpipe(value, [
(_) => _.replace(/,/g, ""),
(_) => parseInt(_)
]);
}

HexNumberValue
= value:$[0-9A-Fa-f]+ {
return parseInt(value, 16);
}

IdentifierValue
= value:$[a-zA-Z_-]+ {


+ 3
- 1
src/packages/exec-smartctl/parsers/shared.pegjs View File

@@ -1,4 +1,6 @@
import { RestOfLine, Newline } from "./primitives"
import { Newline } from "../../peg-newline"

import { RestOfLine } from "./primitives"

Header 'header'
= "smartctl " versionString:RestOfLine "Copyright" copyrightStatement:RestOfLine Newline {


+ 29
- 0
src/packages/peg-grouped-number/index.pegjs View File

@@ -0,0 +1,29 @@
{
function stripCommas(string) {
return string.replace(/,/g, "");
}

function stripPeriods(string) {
return string.replace(/\./g, "");
}
}

CommaDelimitedInteger
= value:$[0-9,]+ {
return parseInt(stripCommas(value));
}

CommaDelimitedFloat
= value:$([0-9,]+ ("." [0-9]+)?) {
return parseFloat(stripCommas(value));
}

PeriodDelimitedInteger
= value:$[0-9.]+ {
return parseInt(stripPeriods(value));
}

PeriodDelimitedFloat
= value:$([0-9.]+ ("," [0-9]+)?) {
return parseFloat(stripPeriods(value).replace(/,/g, "."));
}

+ 4
- 0
src/packages/peg-hex-number/index.pegjs View File

@@ -0,0 +1,4 @@
HexInteger // NOTE: This is assumed to be an unsigned integer!
= value:$[0-9a-zA-Z]+ {
return parseInt(value, 16);
}

+ 3
- 0
src/packages/peg-newline/index.pegjs View File

@@ -0,0 +1,3 @@
Newline
= "\n"
/ "\r\n"

+ 14
- 0
src/packages/peg-number/index.pegjs View File

@@ -0,0 +1,14 @@
Integer
= value:$[0-9]+ {
return parseInt(value);
}

CommaFloat
= value:$([0-9]+ ("," [0-9]+)?) {
return parseFloat(value.replace(/,/g, "."));
}

PeriodFloat
= value:$([0-9]+ ("." [0-9]+)?) {
return parseFloat(value);
}

+ 17
- 0
src/packages/peg-whitespace/index.pegjs View File

@@ -0,0 +1,17 @@
import { Newline } from "../peg-newline"

SameLine
= whitespace:$(" " / "\t")+ {
return whitespace;
}

AcrossLines
= whitespace:$(SameLine / Newline)+ {
return whitespace;
}

SameLineOptional
= SameLine?

AcrossLinesOptional
= AcrossLines?

Loading…
Cancel
Save