From 11f2f2e329ef404d0e9c022cb2f9fbbb45bae285 Mon Sep 17 00:00:00 2001 From: nancy.liao <huihui.liao@greentest.com.cn> Date: 周日, 27 4月 2025 17:33:31 +0800 Subject: [PATCH] 完成了SCPI命令语法分析器的完整规则 --- libscpi/inc/scpi/externinterface.h | 257 +++----------------------------------------------- 1 files changed, 19 insertions(+), 238 deletions(-) diff --git a/libscpi/inc/scpi/externinterface.h b/libscpi/inc/scpi/externinterface.h index 6e7c4db..03d1107 100644 --- a/libscpi/inc/scpi/externinterface.h +++ b/libscpi/inc/scpi/externinterface.h @@ -1,253 +1,34 @@ #ifndef EXTERNINTERFACE_H #define EXTERNINTERFACE_H -#include <iostream> -#include<vector> + +//杩欎釜绫荤敤鍦ㄥ仛SCPI鍛戒护鐨勮娉曞垎鏋�,浼氬皢璇嶆硶鍖归厤鐨勭粨鏋滆繑鍥炵粰SCPI搴� #ifdef __cplusplus extern "C" { #endif -namespace SCPIPASER +typedef struct { -class Segment { -public: - //蹇呭~ - bool is_required = false; - //鍙橀噺 [:Measure] 杩欑鐨勪负鍙橀噺 浼氱疆涓篢RUE - bool is_variable = false; - //鏄惁鏈夊祵濂楀舰寮� - bool is_nested = false; - // 鍙�夌粍鐨勬暟閲� - int variableSize = 0; - std::vector<std::string> options; - // std::vector<Segment> sub_segments; // 鐢ㄤ簬瀛樺偍宓屽鐨勫彲閫夌粍 -}; + int is_required; + int is_variable; + int is_nested; + int variableSize; + char** options; + int options_count; +} CSegment; -class PatternParser +typedef struct { -private: - static void trim_whitespace(std::string& str) - { - auto not_space = [](int ch) { return !std::isspace(ch); }; - str.erase(str.begin(), std::find_if(str.begin(), str.end(), not_space)); - str.erase(std::find_if(str.rbegin(), str.rend(), not_space).base(), str.end()); - } - - static std::vector<std::string> split_options(const std::string& input) { - std::vector<std::string> result; - size_t start = 0; - size_t end = input.find('|'); - - while (end != std::string::npos) { - std::string token = input.substr(start, end - start); - trim_whitespace(token); - //token = remove_colon(token); // 绉婚櫎鍐掑彿 - if (!token.empty()) result.push_back(token); - start = end + 1; - end = input.find('|', start); - } - - std::string last_token = input.substr(start); - trim_whitespace(last_token); - last_token = remove_colon(last_token); // 绉婚櫎鍐掑彿 - if (!last_token.empty()) result.push_back(last_token); - return result; - } - - static std::string remove_colon(const std::string& input) { - std::string result; - for (char ch : input) { - if (ch != ':') { - result += ch; - } - } - return result; - } -public: - static std::vector<std::string> parse_input_data(const std::string& input) - { - std::vector<std::string> result; - size_t start = 0; - size_t end = input.find(':'); - - while (end != std::string::npos) - { - result.push_back(input.substr(start, end - start)); - start = end + 1; - end = input.find(':', start); - } - - result.push_back(input.substr(start)); - return result; - } - static bool match_all_segments(std::vector<std::string> stringList, std::vector<Segment> vecSegment) - { - int currentIndex = 0; - for (int i =0;i<vecSegment.size();i++) - { - auto currentSegment = vecSegment[i]; - //涓ユ牸鍖归厤 - if (currentSegment.is_required) - { - for (auto option : currentSegment.options) - { - if (stringList[currentIndex] != option) - { - //std::cout << "鍙傛暟鍖归厤澶辫触 " << stringList[currentIndex] << " option : " << option; - return false; - } - else - { - //std::cout << "鍙傛暟鍖归厤鎴愬姛 " << stringList[currentIndex] << " option : " << option<<std::endl; - currentIndex += 1; - } - break; - } - } - //鍙�夌粍 - else if (currentSegment.is_variable) - { - //濡傛灉鏈夊彲閫夌粍鐨勬儏鍐典笅 涓斿弬鏁伴暱搴︿竴鐩� 鍒欏尮閰嶅彲閫夐」鍐呭 鍚﹀垯鐨勮瘽璺宠繃 - if (stringList.size() == vecSegment.size()) - { - auto vecTempSegments = currentSegment.options; - for (auto option : vecTempSegments) - { - if (stringList[currentIndex] != option) - { - //std::cout << "鍙傛暟鍖归厤澶辫触 " << stringList[currentIndex] << " option : " << option; - return false; - } - else - { - //std::cout << "鍙傛暟鍖归厤鎴愬姛 " << stringList[currentIndex] << " option : " << option << std::endl; - currentIndex += 1; - } - break; - } - } - } - //鍙彉鍙� - else - { - //std::cout << "鍙彉鍙傛暟锛� "<< stringList[currentIndex] << " " << currentSegment.options[0] << std::endl; - currentIndex += 1; - } - } - return true; - } + CSegment* segments; + int segments_count; +} CPatternResult; - static std::vector<Segment> extract_all_segments(const std::string& pattern) - { - std::vector<Segment> segments; - size_t pos = 0; - const size_t len = pattern.length(); - - while (pos < len) - { - while (pos < len && std::isspace(pattern[pos])) ++pos; - if (pos >= len) break; - - if (pattern[pos] == '<') { - size_t end_pos = pattern.find('>', pos); - if (end_pos == std::string::npos) break; - - std::string content = pattern.substr(pos + 1, end_pos - pos - 1); - trim_whitespace(content); - - Segment segment; - segment.is_variable = false; // `<...>` 涓嶈瑙嗕负鍙橀噺 - segment.options = split_options(content); - - // 妫�鏌ユ槸鍚﹀寘鍚� `:` - if (content.find(':') != std::string::npos) { - segment.is_required = true; // 濡傛灉鍖呭惈 `:` - } - else { - segment.is_required = false; // 濡傛灉涓嶅寘鍚� `:` - } - - segments.push_back(segment); - pos = end_pos + 1; - } - else if (pattern[pos] == '[') - { - size_t end_pos = pattern.find(']', pos); - if (end_pos == std::string::npos) break; - - std::string content = pattern.substr(pos + 1, end_pos - pos - 1); - trim_whitespace(content); - - Segment segment; - segment.variableSize += 1; - segment.is_required = false; - segment.is_variable = true; - - segment.options = split_options(content); - - segments.push_back(segment); - pos = end_pos + 1; - } - else - { - size_t next_lt = pattern.find('<', pos); - size_t next_lb = pattern.find('[', pos); - size_t next_special = std::min(next_lt, next_lb); - - std::string content; - if (next_special != std::string::npos) - { - content = pattern.substr(pos, next_special - pos); - } - else - { - content = pattern.substr(pos); - } - trim_whitespace(content); - - if (!content.empty()) - { - // 鐢� ':' 鎷嗗垎鍙傛暟 - size_t start = 0; - size_t end = content.find(':'); - while (end != std::string::npos) - { - std::string token = content.substr(start, end - start); - trim_whitespace(token); - - if (!token.empty()) - { - Segment segment; - segment.is_required = true; // 鏄庣‘鏍囪涓哄繀濉」 - segment.is_variable = false; // 鏅�氭枃鏈笉瑙嗕负鍙橀噺 - segment.options.push_back(token); - segments.push_back(segment); - } - start = end + 1; - end = content.find(':', start); - } - // 澶勭悊鏈�鍚庝竴涓儴鍒� - std::string last_token = content.substr(start); - trim_whitespace(last_token); - if (!last_token.empty()) - { - Segment segment; - segment.is_required = true; - segment.is_variable = false; - segment.options.push_back(last_token); - segments.push_back(segment); - } - } - pos = (next_special != std::string::npos) ? next_special : len; - } - } - - return segments; - } - -}; -} +int match_segments_global(const char* input, int pattern_index); +void parse_pattern_global(const char* pattern); +int get_pattern_count(); +void clear_global_patterns(); #ifdef __cplusplus } #endif + #endif // EXTERNINTERFACE_H -- Gitblit v1.9.1