Refactor Utils (#5)

This commit is contained in:
Piotr Rogowski 2023-10-03 08:57:06 +02:00 committed by GitHub
parent 28f265ccd9
commit 81f2b417fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 56 deletions

View File

@ -1,3 +1,5 @@
import 'package:ini_parser/models/ini_config.dart';
extension Parsing on String {
int parseInt() {
return int.parse(this);
@ -29,4 +31,44 @@ extension Parsing on String {
bool isExpression() {
return startsWith('{') && endsWith('}');
}
ConstantType toConstantType() {
switch (this) {
case 'scalar':
return ConstantType.scalar;
case 'bits':
return ConstantType.bits;
case 'array':
return ConstantType.array;
case 'string':
return ConstantType.string;
default:
throw Exception('Unknown ConstantType: $this');
}
}
ConstantSize toConstantSize() {
switch (this) {
case 'U08':
return ConstantSize.u08;
case 'S08':
return ConstantSize.s08;
case 'U16':
return ConstantSize.u16;
case 'S16':
return ConstantSize.s16;
case 'U32':
return ConstantSize.u32;
case 'S32':
return ConstantSize.s32;
case 'S64':
return ConstantSize.s64;
case 'F32':
return ConstantSize.f32;
case 'ASCII':
return ConstantSize.ascii;
default:
throw Exception('Unknown ConstantSize: $this');
}
}
}

View File

@ -3,7 +3,6 @@ import 'package:ini_parser/models/ini_config.dart';
import 'package:ini_parser/parsing_exception.dart';
import 'package:ini_parser/patterns.dart';
import 'package:ini_parser/section.dart';
import 'package:ini_parser/utils.dart';
import 'package:text_parser/text_parser.dart';
class ConstantsParser {
@ -66,8 +65,8 @@ class ConstantsParser {
void _parseConstant(List<String> result) {
final name = result.first;
final type = Utils.toConstantType(result[1]);
final size = Utils.toConstantSize(result[2]);
final type = result[1].toConstantType();
final size = result[2].toConstantSize();
final offset = result[3].parseInt();
Constant constant;

View File

@ -3,7 +3,6 @@ import 'package:ini_parser/models/ini_config.dart';
import 'package:ini_parser/parsing_exception.dart';
import 'package:ini_parser/patterns.dart';
import 'package:ini_parser/section.dart';
import 'package:ini_parser/utils.dart';
import 'package:text_parser/text_parser.dart';
class OutputChannelsParser {
@ -34,7 +33,7 @@ class OutputChannelsParser {
.toList();
// coolant = { (coolantRaw - 40) * 1.8 + 32 }
if (Utils.isExpression(result[1])) {
if (result[1].isExpression()) {
_outputChannels.channels.add(
OutputChannelDynamic(
name: result[0],
@ -57,8 +56,8 @@ class OutputChannelsParser {
void _parseChannel(List<String> result) {
final name = result[0];
final type = Utils.toConstantType(result[1]);
final size = Utils.toConstantSize(result[2]);
final type = result[1].toConstantType();
final size = result[2].toConstantSize();
final offset = result[3].parseInt();
OutputChannel channel;

View File

@ -3,7 +3,6 @@ import 'package:ini_parser/models/ini_config.dart';
import 'package:ini_parser/parsing_exception.dart';
import 'package:ini_parser/patterns.dart';
import 'package:ini_parser/section.dart';
import 'package:ini_parser/utils.dart';
import 'package:text_parser/text_parser.dart';
class PcVariablesParser {
@ -45,8 +44,8 @@ class PcVariablesParser {
void _parsePcVariable(List<String> result) {
final name = result.first;
final type = Utils.toConstantType(result[1]);
final size = Utils.toConstantSize(result[2]);
final type = result[1].toConstantType();
final size = result[2].toConstantSize();
PcVariable pcVariable;
switch (type) {

View File

@ -1,46 +0,0 @@
import 'package:ini_parser/models/ini_config.dart';
class Utils {
static ConstantType toConstantType(String string) {
switch (string) {
case 'scalar':
return ConstantType.scalar;
case 'bits':
return ConstantType.bits;
case 'array':
return ConstantType.array;
case 'string':
return ConstantType.string;
default:
throw Exception('Unknown ConstantType: $string');
}
}
static ConstantSize toConstantSize(String string) {
switch (string) {
case 'U08':
return ConstantSize.u08;
case 'S08':
return ConstantSize.s08;
case 'U16':
return ConstantSize.u16;
case 'S16':
return ConstantSize.s16;
case 'U32':
return ConstantSize.u32;
case 'S32':
return ConstantSize.s32;
case 'S64':
return ConstantSize.s64;
case 'F32':
return ConstantSize.f32;
case 'ASCII':
return ConstantSize.ascii;
default:
throw Exception('Unknown ConstantSize: $string');
}
}
static bool isExpression(String input) =>
input.startsWith('{') && input.endsWith('}');
}