diff --git a/src/packages/exec-smartctl/parsers/commands/attributes.pegjs b/src/packages/exec-smartctl/parsers/commands/attributes.pegjs index f25913c..bbd57f0 100644 --- a/src/packages/exec-smartctl/parsers/commands/attributes.pegjs +++ b/src/packages/exec-smartctl/parsers/commands/attributes.pegjs @@ -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 diff --git a/src/packages/exec-smartctl/parsers/commands/info.pegjs b/src/packages/exec-smartctl/parsers/commands/info.pegjs index 1e1135b..02a98ca 100644 --- a/src/packages/exec-smartctl/parsers/commands/info.pegjs +++ b/src/packages/exec-smartctl/parsers/commands/info.pegjs @@ -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 diff --git a/src/packages/exec-smartctl/parsers/commands/scan.pegjs b/src/packages/exec-smartctl/parsers/commands/scan.pegjs index 9d36aea..9084b51 100644 --- a/src/packages/exec-smartctl/parsers/commands/scan.pegjs +++ b/src/packages/exec-smartctl/parsers/commands/scan.pegjs @@ -1,4 +1,6 @@ -import { _, RestOfLine } from "../primitives" +import { SameLine as _ } from "../../../peg-whitespace" + +import { RestOfLine } from "../primitives" RootScan = devices:ScanDevice* { diff --git a/src/packages/exec-smartctl/parsers/primitives.pegjs b/src/packages/exec-smartctl/parsers/primitives.pegjs index c9cb78b..72e7e2c 100644 --- a/src/packages/exec-smartctl/parsers/primitives.pegjs +++ b/src/packages/exec-smartctl/parsers/primitives.pegjs @@ -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_-]+ { diff --git a/src/packages/exec-smartctl/parsers/shared.pegjs b/src/packages/exec-smartctl/parsers/shared.pegjs index daf9bfc..5fba5b5 100644 --- a/src/packages/exec-smartctl/parsers/shared.pegjs +++ b/src/packages/exec-smartctl/parsers/shared.pegjs @@ -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 { diff --git a/src/packages/peg-grouped-number/index.pegjs b/src/packages/peg-grouped-number/index.pegjs new file mode 100644 index 0000000..5cdda8a --- /dev/null +++ b/src/packages/peg-grouped-number/index.pegjs @@ -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, ".")); + } diff --git a/src/packages/peg-hex-number/index.pegjs b/src/packages/peg-hex-number/index.pegjs new file mode 100644 index 0000000..421a532 --- /dev/null +++ b/src/packages/peg-hex-number/index.pegjs @@ -0,0 +1,4 @@ +HexInteger // NOTE: This is assumed to be an unsigned integer! + = value:$[0-9a-zA-Z]+ { + return parseInt(value, 16); + } diff --git a/src/packages/peg-newline/index.pegjs b/src/packages/peg-newline/index.pegjs new file mode 100644 index 0000000..22127af --- /dev/null +++ b/src/packages/peg-newline/index.pegjs @@ -0,0 +1,3 @@ +Newline + = "\n" + / "\r\n" diff --git a/src/packages/peg-number/index.pegjs b/src/packages/peg-number/index.pegjs new file mode 100644 index 0000000..3fa2c70 --- /dev/null +++ b/src/packages/peg-number/index.pegjs @@ -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); + } diff --git a/src/packages/peg-whitespace/index.pegjs b/src/packages/peg-whitespace/index.pegjs new file mode 100644 index 0000000..6b2a1f8 --- /dev/null +++ b/src/packages/peg-whitespace/index.pegjs @@ -0,0 +1,17 @@ +import { Newline } from "../peg-newline" + +SameLine + = whitespace:$(" " / "\t")+ { + return whitespace; + } + +AcrossLines + = whitespace:$(SameLine / Newline)+ { + return whitespace; + } + +SameLineOptional + = SameLine? + +AcrossLinesOptional + = AcrossLines?