mttFetchStruct.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function model = mttFetchStruct(filename)
  2. model.representation = 'struct' ;
  3. mttAssert(mttFileExists(filename),...
  4. ['File "',filename,'" not found']) ;
  5. mttNotify([' ...processing ',filename]) ;
  6. mttWriteNewLine ;
  7. model.source = mttCutText(filename,'_struct.txt') ;
  8. content = mttReadFile(filename) ;
  9. statements = mttExtractStatements(content) ;
  10. number_of_statements = length(statements) ;
  11. next = 0 ;
  12. parsing = 1 ;
  13. while parsing
  14. next = next + 1 ;
  15. statement = statements{next} ;
  16. [keyword,line] = mttSeparateText(statement) ;
  17. switch keyword
  18. case {'struct','public_struct','private_struct'},
  19. struct_name = line ;
  20. mttValidateName(struct_name) ;
  21. [struct,next] = fetch_struct(statements,next,struct_name) ;
  22. model = setfield(model,'item',struct_name,struct) ;
  23. is_private = strcmp(keyword,'private_struct') ;
  24. model = setfield(model,'item',struct_name,'is_private',is_private) ;
  25. case '{',
  26. error('Unexpected "{" found') ;
  27. case '}',
  28. error('Unexpected "}" found') ;
  29. otherwise,
  30. error(['Unrecognised top-level keyword "',keyword,'"']) ;
  31. end
  32. if next==number_of_statements
  33. parsing = 0 ;
  34. end
  35. end
  36. function [struct,next] = fetch_struct(statements,next,struct_name)
  37. struct = [] ;
  38. unit_name = 'struct' ;
  39. number_of_statements = length(statements) ;
  40. counter = 0 ;
  41. open = 0 ;
  42. parsing = 1 ;
  43. while parsing
  44. next = next + 1 ;
  45. statement = statements{next} ;
  46. [keyword,line] = mttSeparateText(statement) ;
  47. switch keyword
  48. case 'var',
  49. mttAssert(open,...
  50. ['"var" declarations must be contained inside {...} in "',unit_name,'"']) ;
  51. mttAssert(~isempty(line),...
  52. ['Undefined "var" in "',unit_name,'"']) ;
  53. data_name = line ;
  54. mttValidateName(data_name) ;
  55. counter = counter + 1 ;
  56. struct.var(counter).name = data_name ;
  57. struct.var(counter).type = [] ;
  58. case '{',
  59. mttAssert(~open,['Unmatched "{" in "',unit_name,'"']) ;
  60. open = 1 ;
  61. case '}',
  62. mttAssert(open,['Unmatched "}" in "',unit_name,'"']) ;
  63. open = 0 ;
  64. otherwise,
  65. mttAssert(open,...
  66. ['Declarations must be contained inside {...} in "',unit_name,'"']) ;
  67. mttAssert(~isempty(line),...
  68. ['Empty declaration in "',unit_name,'"']) ;
  69. data_name = line ;
  70. mttValidateName(data_name) ;
  71. data_type = keyword ;
  72. mttValidateName(data_type) ;
  73. counter = counter + 1 ;
  74. struct.var(counter).name = data_name ;
  75. struct.var(counter).type = data_type ;
  76. end
  77. mttAssert(~(open & (next==number_of_statements)),...
  78. ['Missing "}" in "',unit_name,'"']) ;
  79. if (~open) | (next==number_of_statements)
  80. parsing = 0 ;
  81. end
  82. end